Results 1 to 6 of 6

Thread: global variables

  1. #1
    almaha is offline VB.NET Forum Newbie
    .NET Framework
    .NET 2.0
    Join Date
    Aug 2010
    Posts
    7
    Reputation
    0

    global variables

    Hi,
    How I can declare global variables in vb.net that I can access them in all windows of my application?
    Last edited by almaha; 08-16-2010 at 6:39 PM.

  2. #2
    kulrom's Avatar
    kulrom is offline VB.NET Forum All-Mighty
    .NET Framework
    .NET 3.5
    Join Date
    May 2005
    Location
    Republic of Macedonia
    Posts
    2,855
    Reputation
    236
    You can use modules or even better classes (need to be instantiated as an object before use).
    If you go for classes with shared members you don't have to instantiate them before use.
    If i was you i would probably go for modules as they are more easier to be used.

  3. #3
    lordofduct is offline VB.NET Forum Enthusiast
    .NET Framework
    .NET 3.5
    Join Date
    Jun 2010
    Posts
    71
    Reputation
    53
    be warned, global variables can lead to some very bad design choices. Be very careful with them.

    I use them for very few things... notably I use them for:

    Constants
    Logging methods
    Utility Methods (as Shared/Static member classes)... like a Math class or something.

  4. #4
    jmcilhinney's Avatar
    jmcilhinney is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    9,709
    Reputation
    1415
    As lordofduct suggests, global variables should generally be avoided. If you explain why you want this global variable then we can explain the best option.

  5. #5
    almaha is offline VB.NET Forum Newbie
    .NET Framework
    .NET 2.0
    Join Date
    Aug 2010
    Posts
    7
    Reputation
    0
    l have some variables initialized in one window and need to be check from another windows , that's why i need global variables

  6. #6
    JohnH's Avatar
    JohnH is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Dec 2005
    Location
    Norway
    Posts
    13,315
    Reputation
    1991
    That depends on context, but usually you pass the information on through properties or as method arguments, going with the program flow.
    Quote Originally Posted by almaha
    l have some variables initialized in one window and need to be check from another windows
    This doesn't really doesn't explain much about the context, or the program flow. For example, if one window is shown as a dialog from another, then you can pass info to dialog before shown and retrieve info from dialog when it returns. As independent windows, if one shows another it may pass the info at that time if available. If the info is not available at that time you may pass the form reference so that the other window can retrieve the info when and if it may become available. If using single instance forms you can also in such scenario use the default form instance reference to access the other window.

    As for using global variables (in modules) I'd say that would be as convenience in application scope where the data is single instance, and the state of the data is not relevant in any context; ie there would not be a dependency about what is put there, who puts it there, when it is put there, and when it is retrieved.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Search Engine Optimization by vBSEO