Results 1 to 15 of 15

Thread: Making variable global in .Net

  1. #1

    Thread Starter
    Lively Member abhaybakshi's Avatar
    Join Date
    Dec 2001
    Location
    Mumbai (INDIA)
    Posts
    81

    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...

  2. #2
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985
    Same way

  3. #3
    Lively Member markmyb's Avatar
    Join Date
    Nov 2001
    Location
    Alberton - South Africa
    Posts
    108
    or add a module, in the module create a public varaible.
    Confusios say "Man who run in front of car get tired."

  4. #4
    Lively Member ayan's Avatar
    Join Date
    Jan 2004
    Posts
    112
    you can use module... this much serves as .h files in c...

  5. #5
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985
    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.

  6. #6

    Thread Starter
    Lively Member abhaybakshi's Avatar
    Join Date
    Dec 2001
    Location
    Mumbai (INDIA)
    Posts
    81

    Angry

    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...

  7. #7
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    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.

  8. #8
    Hyperactive Member
    Join Date
    Jun 2001
    Location
    Buffalo, NY
    Posts
    297
    For Example:

    Code:
    Module modGlobals
    
       Public MyGlobalString As String
    
    End Module
    
    Public Class MyForm
    ...
    End Class

  9. #9
    Lively Member
    Join Date
    Nov 2003
    Location
    Iowa
    Posts
    96
    What i do is.. after the windows generated code type in

    VB Code:
    1. 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!

  10. #10
    Hyperactive Member ARPRINCE's Avatar
    Join Date
    Mar 2003
    Location
    Pinoy in NJ
    Posts
    381
    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.

  11. #11
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    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.

  12. #12
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840
    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...

  13. #13
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    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.

  14. #14
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    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.

  15. #15
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840
    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
  •  



Click Here to Expand Forum to Full Width