Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Geek Culture / Visual Basic Variables

Author
Message
Edizzle
20
Years of Service
User Offline
Joined: 22nd Aug 2004
Location:
Posted: 25th Sep 2004 09:43
Hi i just got visual basic and i CANNOT figure out variables
heres my code

Private Sub Form_Load()
Dim x As Integer
x = 1
Label1 = x
End Sub

Private Sub Command1_Click()
x = x + 1
Label1 = x
End Sub

^^^ will not work at all the variable does not increase each click it just makes the label box blank not even 0 just blank please help
i've dicked around with it 100 times and nothing what am i leaving out WHY WOUN'T YOU WORK!!!!! FOR THE LOVE OF GOD WHYYYYY



http://edizzle.100free.com
Giles Papworth
21
Years of Service
User Offline
Joined: 7th Feb 2003
Location: In my own little reality
Posted: 25th Sep 2004 09:52
right the variable x is local to the sub Form_Load so the sub Command1_click cannot access that variable.
i have put some code in for your viewing pleasure

The dim x as integer must go outside any sub routines (ie in the general decleratiosn part of the form)

Its not a bug, Its a feature

If we were meant to stay in this reality games would not exist!
Giles Papworth
21
Years of Service
User Offline
Joined: 7th Feb 2003
Location: In my own little reality
Posted: 25th Sep 2004 10:07 Edited at: 25th Sep 2004 10:08
if you didn't look at the code here it is again. You can leave out the 'Option Explicit' by the way if you like


Its not a bug, Its a feature

If we were meant to stay in this reality games would not exist!
hyrichter
20
Years of Service
User Offline
Joined: 15th Feb 2004
Location: Arizona
Posted: 25th Sep 2004 10:47 Edited at: 25th Sep 2004 10:54
Another thing that would be better would be label1.caption=x instead of label1=x. When I first saw that code, I didn't even think it would run, but I tried it with VB an it worked. (I guess the caption property is the default of label controls)

If you're confused with VB, I highly suggest getting "Sams Teach yourself Visual Basic 6 in 21 Days"(If you're using VB6) Or just look online for some VB tutorials. Once you get the hang of VB, you'll love it.

Edit: May I introduce you to static variables? A static variable is exactly what you need for something like this. When you declare a variable with the "dim" statement it is set to 0 or null as the case may be. So every time you click the Command button, x will be set to 0 again. However, by using a static variable, the variable retains the same value. Here's an examle. You click the command button and the caption on the label increases by one.



Giles Papworth
21
Years of Service
User Offline
Joined: 7th Feb 2003
Location: In my own little reality
Posted: 25th Sep 2004 10:57
Now he knows about variable scope and static variables, yay

Its not a bug, Its a feature

If we were meant to stay in this reality games would not exist!
CattleRustler
Retired Moderator
21
Years of Service
User Offline
Joined: 8th Aug 2003
Location: case modding at overclock.net
Posted: 25th Sep 2004 11:50
yay


DBP_NETLIB_v1.4.3 DarkTOPIA site coming soon!
Edizzle
20
Years of Service
User Offline
Joined: 22nd Aug 2004
Location:
Posted: 27th Sep 2004 09:24
yah i do i didn't know if you make a variable in a sub it only worked in that sub. thanks a lot. and thanks hyrichter for the static command i understand that too.

http://edizzle.100free.com

Login to post a reply

Server time is: 2024-11-26 04:19:54
Your offset time is: 2024-11-26 04:19:54