Results 1 to 5 of 5

Thread: Global Variables in C#

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2002
    Location
    London
    Posts
    63

    Question Global Variables in C#

    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?

  2. #2
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    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.
    Dont gain the world and lose your soul

  3. #3
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,539
    u can use static variables...

    here is an example

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

    PHP Code:

        
    /// <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.

  4. #4
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    I have something you might be interested posted at my site:

    http://www.russellsplace.com/variant...ptutorials.htm

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

  5. #5

    Thread Starter
    Member
    Join Date
    Sep 2002
    Location
    London
    Posts
    63

    Thumbs up

    Thanks for everyones help. That was great.

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