Results 1 to 3 of 3

Thread: [RESOLVED] Global variable problem.

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2002
    Location
    midewest u.s.
    Posts
    275

    Resolved [RESOLVED] Global variable problem.

    Hey everyone,

    Sorry for the dumb question, but I cannot figure this out for the life of me.
    I have searched the forums for an answer to this problem and every thread I have found says I need to create a class and declare the variable as static.

    I created a new class file, and created a class inside of the programs namespace. I declared the variable as a public static string. When I try to use the variable in another file (main form) I get an error stating "The name 'g_szConnString' does not exist in the class or namespace SQLServer.Form1."

    My program name (therefor namespace) is SQLServer, and I am using it in the Form1 class. Is there something I am missing? Do I need to create an instance of the globals class in all of the files that will be using it?

    Thanks in advance.

    Here are a couple code snipptes.

    Code:
    /* Global variable class */
    namespace SQLServer
    {
    	public class CGlobal
    	{
    		public static string g_szConnString = "Data Source=[server];Initial Catalog=[db];User ID=[user];Password=[pass]";
    	}
    }
    Code:
    /* The code that uses the variable */
    SqlConnection   sqlCon = new SqlConnection(g_szConnString);
    Last edited by Tool; Nov 24th, 2006 at 11:46 PM. Reason: Resloved.

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

    Re: Global variable problem.

    Code:
    SqlConnection sqlCon = new SqlConnection(CGlobal.g_szConnString);
    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

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2002
    Location
    midewest u.s.
    Posts
    275

    Re: Global variable problem.

    Ah! Thank you very much jmcilhinney, I didn't know you had to type classname. before the variable to be used. I miss #include or modules in vb. I appreciate the fast response, that will save me a lot of time.

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