How to make a variable accessible to the whole application?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • adodotnet20@gmail.com

    #1

    How to make a variable accessible to the whole application?

    I'm developing a Windows application and I'm having some problems in
    making variable accessible to the whole application. I use VS.NET 2005
    and I create a database connection object in the method private void
    connect_Click(o bject sender, EventArgs e). That object I create seems
    to be accessible only within that method, how do I make it accessible
    to the whole application, just like I have access to mytextbox.text
    throughout my tabbed form? Where do you keep your connection object
    usually?

    Thank you.

  • Mr Icekirby

    #2
    RE: How to make a variable accessible to the whole application?

    C# does not support global variables. You can declare variables outside of
    functions. These are called member variables.

    class Fake
    {
    public int MyInt;
    }

    Anything accessing Fake.MyInt can read it. If you have a function in the
    class, it can also read MyInt.

    "adodotnet20@gm ail.com" wrote:
    I'm developing a Windows application and I'm having some problems in
    making variable accessible to the whole application. I use VS.NET 2005
    and I create a database connection object in the method private void
    connect_Click(o bject sender, EventArgs e). That object I create seems
    to be accessible only within that method, how do I make it accessible
    to the whole application, just like I have access to mytextbox.text
    throughout my tabbed form? Where do you keep your connection object
    usually?
    >
    Thank you.
    >
    >

    Comment

    • Ben Voigt

      #3
      Re: How to make a variable accessible to the whole application?


      <adodotnet20@gm ail.comwrote in message
      news:1162924843 .343653.111310@ h48g2000cwc.goo glegroups.com.. .
      I'm developing a Windows application and I'm having some problems in
      making variable accessible to the whole application. I use VS.NET 2005
      and I create a database connection object in the method private void
      connect_Click(o bject sender, EventArgs e). That object I create seems
      to be accessible only within that method, how do I make it accessible
      to the whole application, just like I have access to mytextbox.text
      throughout my tabbed form? Where do you keep your connection object
      usually?
      You would do that with a class member variable.

      You are not ready for database access if you are unfamiliar with basic
      programming concepts such as scope. I suggest that you complete a tutorial
      that teaches structured programming and object-oriented programming in C#
      before continuing work on your application.
      >
      Thank you.
      >

      Comment

      • C# Beginner

        #4
        Re: How to make a variable accessible to the whole application?

        I'm one of those guys who don't like public variables. My solution is this:
        Create a database class in which you store database members such as
        connectionstrin g property/variable, Open() methods etc. In the starting form
        I create an instance of this database class. When another form needs access
        to the database, I parse the instance using the tag property tag of the
        form.

        NewForm.Tag = (object) myDatabase;

        Within the new form you can use
        .... = (Database) this.tag.

        hope you can use it

        DD
        <adodotnet20@gm ail.comschreef in bericht
        news:1162924843 .343653.111310@ h48g2000cwc.goo glegroups.com.. .
        I'm developing a Windows application and I'm having some problems in
        making variable accessible to the whole application. I use VS.NET 2005
        and I create a database connection object in the method private void
        connect_Click(o bject sender, EventArgs e). That object I create seems
        to be accessible only within that method, how do I make it accessible
        to the whole application, just like I have access to mytextbox.text
        throughout my tabbed form? Where do you keep your connection object
        usually?
        >
        Thank you.
        >

        Comment

        • Peter Bromberg [C# MVP]

          #5
          RE: How to make a variable accessible to the whole application?

          Just add a separate class file to your application and add in all the
          required fields as public with the "static" modifier.
          Then you can refer to these from anywhere with "MyStuff.FieldN ame".
          Peter
          --
          Co-founder, Eggheadcafe.com developer portal:

          UnBlog:





          "adodotnet20@gm ail.com" wrote:
          I'm developing a Windows application and I'm having some problems in
          making variable accessible to the whole application. I use VS.NET 2005
          and I create a database connection object in the method private void
          connect_Click(o bject sender, EventArgs e). That object I create seems
          to be accessible only within that method, how do I make it accessible
          to the whole application, just like I have access to mytextbox.text
          throughout my tabbed form? Where do you keep your connection object
          usually?
          >
          Thank you.
          >
          >

          Comment

          Working...