Click to See Complete Forum and Search --> : Global Variable Question
jeric_mandrake
Mar 13th, 2007, 10:29 AM
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?
penagate
Mar 13th, 2007, 10:32 AM
Global variables are considered bad practice in object-oriented languages. You can make a static property, if that's what you want.
jeric_mandrake
Mar 13th, 2007, 10:40 AM
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?
penagate
Mar 13th, 2007, 10:47 AM
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:
class Form1 : Form
{
// ... somewhere:
private void mymethod()
{
Form2.blah = something;
}
}
This is good:
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.
jeric_mandrake
Mar 13th, 2007, 10:55 AM
thanks sir. I need to learn mo about OOP concepts.. ehehehe. thanks sir.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.