Hello,
I am new to .net.
I want to make one variable global in vb.net. In vb6 i used to declare it in general section at the top. How can i achieve this in .net ?
thanks
abhay
Printable View
Hello,
I am new to .net.
I want to make one variable global in vb.net. In vb6 i used to declare it in general section at the top. How can i achieve this in .net ?
thanks
abhay
Same way
or add a module, in the module create a public varaible.
you can use module... this much serves as .h files in c...
He wanted to declare it at the top of his source. He can do that in VB.NET as well and it'll be available for the entire class it is in. Making it public will allow it to be accessed from other classes.Quote:
Originally posted by markmyb
or add a module, in the module create a public varaible.
I try to stay away from global variables. My very first VB.NET project had a module with about 20 globals in it. Since then, I started re-writing it in C# and I use zero globals. I'd say almost everytime a global is used, it doesn't need to be global. I'm sure there might be some exceptions, but rare ones.
nope.
if i try to define global variable at top, it gives error like "statement is not valid in namespace".....
Let me clarify you must put it at the top of a class/module not at the top of a file. Files aren't a sole class anymore so you have to be within a Public Class yaday and End Class, not in the black area above the first Public Class statement.
For Example:
Code:Module modGlobals
Public MyGlobalString As String
End Module
Public Class MyForm
...
End Class
What i do is.. after the windows generated code type in
VB Code:
Public MyString As String
That will make it global, the word is Public not Global(in VB.NET)
I was wondering why I was having that "not valid in namespace" problem when it used to work for VB6. Thanks for this. :thumb:Quote:
Originally posted by BenFinkel
For Example:
Code:Module modGlobals
Public MyGlobalString As String
End Module
Public Class MyForm
...
End Class
Hi,Quote:
Originally posted by abhaybakshi
nope.
if i try to define global variable at top, it gives error like "statement is not valid in namespace".....
You are getting a couple of confusing replies here.
Put the Public declaration of the Variable immediately after the "Inherits " statement at the top of the form but before the region containing the Windows Generated code and before any other code you provide.
If you decide to put it in the Module, there is no "Inherits" statement nor Window generated code visible in a module.
That's not neccesary (sp?). You can make them anywhere within the class (except inside subs).Quote:
Originally posted by taxes
Put the Public declaration of the Variable immediately after the "Inherits " statement at the top of the form but before the region containing the Windows Generated code and before any other code you provide.
You could make the statement between 2 subs if you wanted, and it would still work the same.
Hi Pax,
"That's not neccesary (sp?). You can make them anywhere within the class (except inside subs).
You could make the statement between 2 subs if you wanted, and it would still work the same."
You are right:eek: I did not know that. Thanks
Pax is right, but having your vars all over the place is confusing. I have started making it a habit to declare a Region for my variables, declaring everything in there.
Agreed :thumb: !Quote:
Originally posted by mendhak
Pax is right, but having your vars all over the place is confusing. I have started making it a habit to declare a Region for my variables, declaring everything in there.
I was merely stating the fact that is was possible.
IMHO the space between subs are for comments, but thats just me.