Click here to Skip to main content
16,020,114 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
Hi,

I have a method to which i need to pass a global variable so that same variable can be used in other methods using vb.net

Please suggest
Posted
Comments
Zoltán Zörgő 31-May-13 7:03am    
global variable in .net???
Please post your code with some explanation.
You can use class level properties and fields, that can be accessed by all methods. Or you can use static properties or fields that can be accessed across instances too. But there is no such thing as global variable...

Uhhh....If it's a "global" variable, why would need to pass it??
 
Share this answer
 
 
Share this answer
 
Forgot global variables in .NET, there is no such thing; and this is only for good. You need to get rid of such way of thinking, instead of trying to find a workaround for this. Global variables are never needed, period.

However, sometimes you need functionality which you used to have with global variables; and there is a special pattern called singleton: http://en.wikipedia.org/wiki/Singleton_pattern[^].

A good implementation is shown here: http://csharpindepth.com/Articles/General/Singleton.aspx[^].

I saw a number of really bad implementations, so pay a special attention for this article in every detail: this is really good.

But even the singleton is often overused by some, so, always start with thinking of the solution not using any; use the singleton only if it is absolutely necessary, as a last resort.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900