|
-
Oct 12th, 2005, 08:19 AM
#1
Thread Starter
Fanatic Member
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.
-
Oct 12th, 2005, 08:36 AM
#2
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.
-
Oct 12th, 2005, 07:56 PM
#3
Fanatic Member
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.
-
Oct 12th, 2005, 09:50 PM
#4
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.
-
Oct 12th, 2005, 11:38 PM
#5
Frenzied Member
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
-
Oct 12th, 2005, 11:54 PM
#6
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.
-
Oct 13th, 2005, 02:07 AM
#7
Thread Starter
Fanatic Member
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!
-
Oct 13th, 2005, 02:15 AM
#8
Thread Starter
Fanatic Member
Re: How can i define a global variable for the whole solution?
Can you please give an example ?
 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.
-
Oct 13th, 2005, 02:21 AM
#9
Frenzied Member
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.
-
Oct 13th, 2005, 06:17 PM
#10
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.
-
Oct 13th, 2005, 09:10 PM
#11
Frenzied Member
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.
-
Oct 14th, 2005, 11:03 AM
#12
New Member
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.
-
Oct 14th, 2005, 11:49 AM
#13
Re: How can i define a global variable for the whole solution?
 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?
-
Oct 14th, 2005, 05:41 PM
#14
Re: How can i define a global variable for the whole solution?
 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.
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
|