Okay, I'm not sure if it's possible, but here goes:
I know that in VB6, it was possible to declare a variable in the declarations (at least, I always did, and it worked) and then it would be there for the entire program.
However, converting one of my projects over into VB2005, I have run into an error. In all of my subs, the variables are now "privately" declared, and I have no idea how or why it doesn't work anymore.
My coding resembles this:
dim a, b, c, d, e as decimal
Private Sub Button_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Button.Click Call determine_variables(a,b,c,d,e)Call show_variables(a,b,c,d,e)
End Sub
Sub determine_variables(ByVal a as decimal, ByVal b as decimal, ByVal c as decimal, ByVal d as decimal, ByVal e as decimal)
a = 1
b = 2
c = 3
End Sub
Sub show_variables(ByVal a as decimal, ByVal b as decimal, ByVal c as decimal, ByVal d as decimal, ByVal e as decimal)
d = a / b
e = a / c
label1.text = d
label2.text = e
End Sub
Now, my program is much more complex, but this is just to show you what is happening. When I run the program to debug, it says that I'm trying to divide by zero, when I have clearly given the variables values in the other Sub command. And again, I must state my program is much more complex than this, so that using Sub commands (or something similar, if you can suggest it) is much easier to code than putting all the code into the Button_Click command.

universal variables
jnquinn
Well.....yes I can see that but then you get modifiers like Protected and Friend. They aren't very intuitive are they - not to me anyway.
Any views on the second bit - is it a mistake or is there something there I really don't understand.
RalfCJ
Sj, I apologize for my lack of clarity and didn't direct my comment to you. I was referring to dave who said local variables default to public.
It just occurred to me that I never use DIM statements for member variables. I will either will invariabley explicitly declare each variable and specifically declare its scope. So actually what dave and I both said could be true at the same time. I've never seen a local variable default to public because I never use DIM for memeber variables.
gkostel
GroZZleR
I don't understand that in terms of what I experience in coding. I've never seen that occur.
TOM_MUE
Another point is the use of Dim: are these variables public or private Unless the variable is used within a subroutine (where the variables are private by definition, or static), you should always declare the scope of the variables, e.g.:
Public a ,b ,c ,d ,e as Decimal
redhot2006
Probably, I dunno...but of course you would have to know that. Which is the problem. That's why it's better to declare them explicitly, so you 'know' just by looking at it.
Even so, the origional problem is actually that the 'global' variables aren't being touched at all: the locl variables are being used (specifically, the passed parameters):
Private a As Decimal< xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
Private b As Decimal
Private Sub DoStuff()
a = 1
b = 2
Call ChangeThings(a, b)
Debug.WriteLine(String.Format("A={0} B={1}", a, b))
End Sub
Private Sub ChangeThings(ByVal a As Single, ByVal b As Single)
a = 5.76543
b = 12.2232115
End Sub
What is the result i.e. what will get printed by the debug line
is it:
A=1 B=2
or:
A=5.76543 B=12.22321
The moral of the story is to use meaningful names, and identify the global variables (often the global variables are prefixed with something like m_ for module level variables, or g_ for application global variables): whatever scheme floats your boat. Personally, I've got away from the m_ and simply have global variables pascal cased, and local variables camel cased - it saves a LOT of headaches.
kimo
First of all get of the "calls". They aren't needed.
Dim a, b, c, d, e As Decimal
Private Sub determine_variables(ByRef a As Decimal, ByRef b As Decimal, ByRef c As Decimal, ByRef d As Decimal, ByRef e As Decimal)
a = 1
b = 2
c = 3
End Sub
Private Sub cbGo_Click(ByVal sender As Object, ByVal eeeeeee As System.EventArgs) Handles cbGo.Click
determine_variables(a, b, c, d, e)
show_variables(a, b, c, d, e)
End Sub
Sub show_variables(ByVal a As Decimal, ByVal b As Decimal, ByVal c As Decimal, ByVal d As Decimal, ByVal e As Decimal)
d = a / b
e = a / c
End Sub
a,b,c, etc are Value Types. In order to chnage their value in a subroutine you need to address them by reference.
pst_grant
You're right, the additional modifiers can get confusing (I'm always saying to myself "what does Protected mean, again I'm sure Mr. Eff One will tell me..."). Just wait 'til you get into overloads, overrides, notinheritable, shadows, etc.... :)
Even so, by positively declaring the scope of the variable will mean the computer is doing what you told it to do, rather than what you think it should do...(I think...).
Cout60039
Woah
I didn't say locals default to Public - that's what the Help and MSDN say.
I don't understand what it means. Can anyone clarify
molnarcs
Ummm... okay. Let me get this straight:
In order to change the values of the variables in the sub routines, I need to put "ByRef" instead of "ByVal"
I also need to put "Public" instead of "dim" when I declare my variables, to make sure they are public and accessable by the entire program
Thanks... I understood a lot of what you said, but some of it just went over my head. :p
Thankyou for helping a poor, backwards, VB6 user. ;)
Lechal
Do you really need to pass the variables to your procedures.
For your simple example the following works just as well.
Dim a, b, c, d, e As Decimal
Private Sub Button_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Button.Click
determine_variables()
show_variables()
End Sub
Sub determine_variables()
a = 1
b = 2
c = 3
End Sub
Sub show_variables()
d = a / b
e = a / c
Label1.Text = d
Label2.Text = e
End Sub
Alexander Marinov
Dave, I agree it works as well.
There's a problem not at all with what you did but with this conceptually, I use member variables all the time. When I dod I ealize that it's very close to a Fortran Common which has been made fun of for several decades and I am an ex-Fortran programmer. I have to admit that I love to use member variables and they do have lots of uses. I always feel guilt at my enjoyment of them because they do function and are used exactly in the same way a fortran common is used.
bsun
Aren't
Dim A As Decimal Private A As Decimalthe same thing at module level
According to the Help file local variables default to Public access - which is something I've never understood.
Thymen
Drake there are two ways to go as Dave and I pointed out. As dave point out there is no reason to use member variables in a call because the member variable are already "inscope" or visible to the called methods. Actually it slows the code down to use them in calls because the machine has to either copy the value variable or propagate references to them on the machine's stack to make a call frame.
ANY TIME you want to change a passed Value Type such as an Integer or string, pass it by reference (byref) because essentially that's a point to the address in memory containing the data. If you pass a Value type to a method byvalue, a copy is made and if you modify the copy that has the results that you saw last night.
The "correct" way to do this would be to do as Dave recommended and he addressed that. I addressed why your code failed.