global variable

Hi friends
i want to create global variable that can be used anywhere in the project.
what i am doing is am creating a connection to a sql server and i want to use that connection thru out my project whenever i want data from sql server.
how can i do that any ideas
where should declare that variable !!!
Thanks


Answer this question

global variable

  • Alain Bismark

    Hi,

    C# is language that has been built after C++, so some of the features of C++ which were considered harmful have been eliminated such as global variables and multiple inheritance.

    Also since C# is a pure Object Oriented Language, everything is an object/class and you cannot directly have a variable outside of a class. So the correct way is to declare a class and have something as a static variable.

    Also since you are specifically speaking of Connection, you could consider using the Data Access Application Block in your application:
    http://msdn.microsoft.com/library/default.asp url=/library/en-us/dnpag2/html/daab.asp

    Regards,
    Vikram

  • Danny_40

    Hi,

    Is the file that you downloaded called "Enterprise Library - August 2005 CTP.zip" This works with VS2005 Beta 2.

    Also This release does not come with a installer - there is only batch files within the QuickStart Samples which help to build the sample.

    Regards,
    Vikram

  • Grahame2005

    Thank u very much.you are wonderful.
    ILL give data access app block a try .

  • Olivier.Destrade

    Hi,

    I was not sure if you were using VS2003 or VS2005. For v2.0, there's a new set of blocks that have been released and thats a CTP release.

    You can download it from here:

    http://www.gotdotnet.com/codegallery/releases/viewuploads.aspx id=295a464a-6072-4e25-94e2-91be63527327

    But I think you need to register on the site for free before u can download.

    Regards,

    Vikram



  • Varadaraj R

    Hi vikram

    i just installed latest enterprise library (microsoft patterns & practices) software.

    after installation some of its items greyed out (like "build enterprise library,enterprise library documentation etc.,) whenever i click them i get command window with an error "could not locate vs 2003" and asks me change bat file with vs2003 location !!

    but am using VS2005 sp2 and tried to change bat file and pointed it to VS2005 folder but no avail.

    BTW i looked sample codes provided by this installation but they all look complex to understand for beginner.
    am wondering if you know sample examples (that r simple) on using data access application block ,Vikram.
    Thanks



  • Jedi21

    i am sorry to inform you but unlike C++, you cannot declare a global string.

    However you can do the following to bypass that:


    public static string something = "hello, this is a global string :D";


    since this is a static, it can be accesses anywhere, remember to include the class to be able to access it, like:


    class NewClass
    {
        public static string something  = "this is a beautiful test";
    }
    class AnotherClass
    {
        public void EnterData(string enter)
        {
            //do something
            NewClass.something = "New Value";
        }
    }


    So there you go :)


  • PSmock

    actually thats what i installed vikram.

    when i went gotdotnet site it has a link to pattern and practices enterprise library which you already provided in the last post.

    anyway i installed again but still same issue.

  • grilledtomatoes

    so it has to be a property in a class ,is it
    Thanks.
    BTW any idea why there is no concept of global variable in C#

  • global variable