Friday, July 31, 2009

How do I declare a global variable in a module in Visual Basic 2005?

I need to declare a variable inside a module so that all code in the program (including code in the main form class) can "see" it.

How do I declare a global variable in a module in Visual Basic 2005?
using System;


using System.Data;


using System.Configuration;


using System.Collections;


using System.Web;





namespace ......................


{





declare here ......





public partial class classname


{}





if it won't work then.....





or...





set properties to that variable.....





so that you can call it anywhere in the application.....with the instance of the class....





if you don't know how to set properties then ask me i will tell you...
Reply:Well, to put it bluntly, you don't.


It is poor programming practice to define a variable that can be accessed so easily from anywhere within a program. Yes, it is done in VBA and such, but it is not good practice.





The correct way to accompish this is to either pass the variable between classes, or to define it as an attribute of the class, and then access it through gets/sets (properties) of an instance of the class.





Yeah - basically what NV said above - he's on the right track.


No comments:

Post a Comment