Results 1 to 5 of 5

Thread: Global Variable Question

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    87

    Global Variable Question

    In VB.NET, I usually declare my variable to a module to make it global. In C#, how can i make my variable global?! can give me a sample code?
    I Hope I Could be Great Like You

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Global Variable Question

    Global variables are considered bad practice in object-oriented languages. You can make a static property, if that's what you want.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    87

    Re: Global Variable Question

    static property? btw, for example, i will get my data in textbox1 of form1. the variable that will hold the data of tb1 will be declared in a module. After that, if i want to access that in form2 or 5, it'll be easy for me because it is declared globally.

    How will it be in C#? and i think the static you'd mentioned is you cannot get a data from any controls. It is stored in the code anymore. What if I want to change that data?
    I Hope I Could be Great Like You

  4. #4
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Global Variable Question

    I think what you need to do is get out of the "forms talking to each other" mindset that VB allows you to develop. Forms are just classes: they should be developed with reusability in mind, even if you're not going to use them more than once, because it makes them so much easier to deal with.

    (In psuedocode,) this is bad:
    Code:
    class Form1 : Form
    {
      // ... somewhere:
      private void mymethod()
      {
        Form2.blah = something;
      }
    }
    This is good:
    Code:
    Form1 f1 = new Form1(something, somethingelse);
    f1.ShowDialog();
    Form2 f2 = new Form2(f1.someproperty);
    That's how you should treat forms. Put application logic outside them, not within them. Don't move data from a form to another form; expose data from form objects using properties and do the data manipulation outside the form. That's how OOP works: all you are doing, all the time, is using objects.

    If you adhere to this principle, you'll find no need for global variables. If you find yourself missing them, then you're doing something wrong and should readdress your application design.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jan 2006
    Posts
    87

    Re: Global Variable Question

    thanks sir. I need to learn mo about OOP concepts.. ehehehe. thanks sir.
    I Hope I Could be Great Like You

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