PDA

Click to See Complete Forum and Search --> : Global Variables in C#


Genie
Jan 17th, 2003, 04:21 AM
moving from VB6 to C#, is there an equivalent 'mod' module you can add to the project to hold global variables or are global variables not permitted in C#.

If globals are not allowed, then how can I store a connection to the database and use it in different forms and classes?

DevGrp
Jan 17th, 2003, 06:09 AM
Use a utitily class. Make a sealed class with the variables that you want to be global, then use can use that class anywhere in your project.

kovan
Jan 17th, 2003, 06:28 AM
u can use static variables...

here is an example

code came right from the source of my app (iCodeLibrary)



/// <summary>
/// Summary description for GlobalStuff.
/// </summary>
public class GlobalStuff
{
public static string ProgramDirectory = Environment.CurrentDirectory;
public static string AccessDBPath = ProgramDirectory + @"\DB\iCodeLibrary.MDB";
public static string DBType = "Access";
public static string LastAccessedDirectory = @"c:\";
public static string AttachmentsDirectory = ProgramDirectory + @"\Attachments\";
public static string SettingsFile = ProgramDirectory + @"\settings.xml";
public static bool IsSettingChanged = false;
private GlobalStuff()
{
}

}


how do you use them?

just do GlobalStuff.AttachmentDirectory
i put mine in a glass cus its more organized and i know when i am using a global variable or method cus of GlobalStuff.

hellswraith
Jan 17th, 2003, 02:43 PM
I have something you might be interested posted at my site:

http://www.russellsplace.com/variantx/csharptutorials.htm

I use it to store APP settings in the registry. You can modify it easily to store the settings into an XML file.

Genie
Jan 21st, 2003, 09:52 AM
Thanks for everyones help. That was great.:)