|
-
Jan 29th, 2004, 12:08 AM
#1
Thread Starter
Lively Member
Making variable global in .Net
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
Money is a great thing, but it can't purchase satisfaction....
Respect money, but don't allow it to control you...
-
Jan 29th, 2004, 12:32 AM
#2
-
Jan 29th, 2004, 12:48 AM
#3
Lively Member
or add a module, in the module create a public varaible.
Confusios say "Man who run in front of car get tired."
-
Jan 29th, 2004, 12:50 AM
#4
Lively Member
you can use module... this much serves as .h files in c...
-
Jan 29th, 2004, 12:59 AM
#5
Originally posted by markmyb
or add a module, in the module create a public varaible.
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.
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.
-
Jan 29th, 2004, 01:19 AM
#6
Thread Starter
Lively Member
nope.
if i try to define global variable at top, it gives error like "statement is not valid in namespace".....
Money is a great thing, but it can't purchase satisfaction....
Respect money, but don't allow it to control you...
-
Jan 29th, 2004, 01:22 AM
#7
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.
-
Jan 29th, 2004, 10:24 AM
#8
Hyperactive Member
For Example:
Code:
Module modGlobals
Public MyGlobalString As String
End Module
Public Class MyForm
...
End Class
-
Jan 29th, 2004, 04:08 PM
#9
Lively Member
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)
It is like wiping your ass with silk, I love it!
-
Jun 2nd, 2004, 11:12 AM
#10
Hyperactive Member
Originally posted by BenFinkel
For Example:
Code:
Module modGlobals
Public MyGlobalString As String
End Module
Public Class MyForm
...
End Class
I was wondering why I was having that "not valid in namespace" problem when it used to work for VB6. Thanks for this.
-
Jun 2nd, 2004, 11:27 AM
#11
PowerPoster
Originally posted by abhaybakshi
nope.
if i try to define global variable at top, it gives error like "statement is not valid in namespace".....
Hi,
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.
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
Jun 2nd, 2004, 01:27 PM
#12
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.
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.
I wish I could think of something witty to put in my sig...
...Currently using VS2013...
-
Jun 2nd, 2004, 06:21 PM
#13
PowerPoster
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 I did not know that. Thanks
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
Jun 2nd, 2004, 11:34 PM
#14
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.
-
Jun 3rd, 2004, 01:14 AM
#15
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.
Agreed !
I was merely stating the fact that is was possible.
IMHO the space between subs are for comments, but thats just me.
I wish I could think of something witty to put in my sig...
...Currently using VS2013...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|