Results 1 to 14 of 14

Thread: How can i define a global variable for the whole solution?

  1. #1

    Thread Starter
    Fanatic Member alexandros's Avatar
    Join Date
    Oct 2002
    Location
    Milky Way Galaxy
    Posts
    694

    How can i define a global variable for the whole solution?

    Hello !
    Do you know how i can define a global variable for the whole solution?
    I mean something like adding a public variable in a module in VB6.

    This could be a connection string for example.

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: How can i define a global variable for the whole solution?

    The concept of a Public variable doesn't exist in C#. You can use a static variable that functions like a public variable, but it is conceptually different.

    In addition, you wouldn't want to keep a connection open all the time. The .NET framework uses connection pooling which means that you can open and close connections when needed kind of like opening and closing a file.

  3. #3
    Fanatic Member popskie's Avatar
    Join Date
    Jul 2005
    Location
    In my chair
    Posts
    666

    Re: How can i define a global variable for the whole solution?

    yes, u can use public static variable but not like in vb6 that u can use many open recordset in one connection.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How can i define a global variable for the whole solution?

    VB.NET modules are implemented as NotInheritable classes with all Shared members when compiled, so if you create a sealed class with all static members in C# then it will behave in exactly the same way, except that you must qualify each member with the class name in your code, which is not required with a VB.NET module. Note that this will be available to the entire PROJECT, not the entire SOLUTION. Each project compiles to its own assembly, so each is an entirely self-contained entity. Note that you can also store simple values, e.g. strings, numbers, boolean values, in the application's config file and they are then available to the entire application and any libraries it might load.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Frenzied Member StrangerInBeijing's Avatar
    Join Date
    Mar 2005
    Location
    Not in Beijing
    Posts
    1,666

    Re: How can i define a global variable for the whole solution?

    Not sure if it is the "right" way, but for stuff like that I use the App.Config, or if I want to modify the xml file (app.config is xml) during runtime, I rather add a xtra xml file to the project.

    but seeing you wanted a way to create a "global" variable, you would not be able to edit during runtime anyhow, you will use the app.config file

    Add an Application Configuration File to your project.
    ...no wait....google it or have a look here

    cheers
    Install and Configure Eclipse For both Java and PHP development
    Accessible Ajax/jQuery Forms Degrade gracefully with JavaScript Disabled

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How can i define a global variable for the whole solution?

    Note that the ConnectionString property of a connection object is a dynamic property, so you can set it to be saved in the config file from the properties window when you add a connection object to a form in the designer. For non-dynamic properties this should be of interest. Note that if you use the method suggested to edit the config file the changes won't be reflected until you restart the application, as values are read once from the config file at startup only. This means that any edited values must be stored in variables until the app is closed.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Fanatic Member alexandros's Avatar
    Join Date
    Oct 2002
    Location
    Milky Way Galaxy
    Posts
    694

    Re: How can i define a global variable for the whole solution?

    Well i want also a global variable that will be modified at runtime.
    For example after the user logins i want a global variable to hold the
    user access rights and the user's role.
    I cannot do it with app.config since each user that logins will have a different
    role.

    thanks!

  8. #8

    Thread Starter
    Fanatic Member alexandros's Avatar
    Join Date
    Oct 2002
    Location
    Milky Way Galaxy
    Posts
    694

    Re: How can i define a global variable for the whole solution?

    Can you please give an example ?

    Quote Originally Posted by jmcilhinney
    VB.NET modules are implemented as NotInheritable classes with all Shared members when compiled, so if you create a sealed class with all static members in C# then it will behave in exactly the same way, except that you must qualify each member with the class name in your code, which is not required with a VB.NET module. Note that this will be available to the entire PROJECT, not the entire SOLUTION. Each project compiles to its own assembly, so each is an entirely self-contained entity. Note that you can also store simple values, e.g. strings, numbers, boolean values, in the application's config file and they are then available to the entire application and any libraries it might load.

  9. #9
    Frenzied Member StrangerInBeijing's Avatar
    Join Date
    Mar 2005
    Location
    Not in Beijing
    Posts
    1,666

    Re: How can i define a global variable for the whole solution?

    Well, what I would / did do, was include an xml file (what the app.config essentially is!), and use my some xmldocument code to read and write from and to that file.
    Install and Configure Eclipse For both Java and PHP development
    Accessible Ajax/jQuery Forms Degrade gracefully with JavaScript Disabled

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How can i define a global variable for the whole solution?

    http://msdn.microsoft.com/library/de...albasicnet.asp

    Note that the config file is read once at app startup, so any changes made will not be reflected in your config settings until the app is restarted. For that reason you should maintain any values that may or have been changed in variables rather than reading them from the config file each time.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11
    Frenzied Member StrangerInBeijing's Avatar
    Join Date
    Mar 2005
    Location
    Not in Beijing
    Posts
    1,666

    Re: How can i define a global variable for the whole solution?

    =jmcilhinneyNote that the config file is read once at app startup, so any changes made will not be reflected in your config settings until the app is restarted. For that reason you should maintain any values that may or have been changed in variables rather than reading them from the config file each time.
    Yep, what I did is create a class with members representing whatever I store in the config file. Create the class when the app starts up, reading the values in from the xml file, and write back to the xml file when the app closes.
    Worked pretty cool for me. Can do a lot this way round, just use your imagination.
    Install and Configure Eclipse For both Java and PHP development
    Accessible Ajax/jQuery Forms Degrade gracefully with JavaScript Disabled

  12. #12
    New Member
    Join Date
    Oct 2005
    Posts
    1

    Re: How can i define a global variable for the whole solution?

    I read this thread but I'm still lacking a good solution. If I've got a login form that validates the user, I want to store the user name in some place that every form can access. There is no way to do that?

    Is is a matter of storing the user name at the main form level (call login, get username, store in a main form var) and then pass that user name to ever form I call?

    I'm trying to map my method from VB6 to C# and this is a sticking point I'd like to overcome.

    Thanks from the new guy.

  13. #13
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: How can i define a global variable for the whole solution?

    Quote Originally Posted by 3DCrew
    I read this thread but I'm still lacking a good solution. If I've got a login form that validates the user, I want to store the user name in some place that every form can access. There is no way to do that?
    How about using a database table field?

  14. #14
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How can i define a global variable for the whole solution?

    Quote Originally Posted by 3DCrew
    I read this thread but I'm still lacking a good solution. If I've got a login form that validates the user, I want to store the user name in some place that every form can access. There is no way to do that?

    Is is a matter of storing the user name at the main form level (call login, get username, store in a main form var) and then pass that user name to ever form I call?

    I'm trying to map my method from VB6 to C# and this is a sticking point I'd like to overcome.

    Thanks from the new guy.
    The implication is that in VB.NET you would create a public variable in a module and when you get the user name, from whatever source, you assign it to that variable so it's accessible to the entire application, so the equivalent to this in C# is to create a class with a static variable or property to which you assign the user name, thus making it accessible throughout your app.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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