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.
Printable View
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.
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.
yes, u can use public static variable but not like in vb6 that u can use many open recordset in one connection.
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.
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
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.
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!
Can you please give an example ?
Quote:
Originally Posted by jmcilhinney
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.
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.
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.Quote:
=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.
Worked pretty cool for me. Can do a lot this way round, just use your imagination. :)
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.
How about using a database table field?Quote:
Originally Posted by 3DCrew
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.Quote:
Originally Posted by 3DCrew