how do you use Class to hold global variables?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

in the old VB, you can use global variables to hold commonly use data. i'll
like to pass a variables selected by user in the combobox, how do you hold
this variable for other object to use. does anyone have an example?
 
You can use a static property.

For example:

public class Global {
private Global() {}

public static string UserName;

}
 
Hello dotnetnoob,

You can create a module, and declare Global variables in the module just
the way you did in the old VB. Classes are not actually meant as holders
of global variables.

Just add a module to your project and declare a variable like this

Public myVariable as Integer

Regards
Cyril Gupta

You can do anything with a little bit of 'magination.
I have been programming so long, that my brain is now soft-ware.
http://www.cyrilgupta.com/blog
 
thank all for reply.

how do you pass commonly use variable in C# then?

so i can add a module in my program to hold the data selected by user in
combobox and use it for other object to access, am i correct?
 
Steven said:
You can use a static property.

For example:

public class Global {
private Global() {}

public static string UserName;

}

Sorry I thought I was in the C# group.
Here's the VB code:

public class Global
public shared string UserName
end class
 
Hi there,

Sorry but that is just not true. You can object orientate anything. In
fact I'd rather do that than use modules any day.

In my honest opinion a shared class is much better than a module,

-------------------------

Public Class GlobalObjects

'//Hide the constructor as it is not needed
Private Sub New()
End Sub

Public Shared gBlnDebugMode As Boolean

End Class
 
Just as an addition: Depending on how you plan to use the variable, you
may want to consider wrapping the variable in a public shared property.
This comes in handy for things like validating entries, error trapping,
etc...

Thanks,

Seth Rowe
 
Funny, I always thought modules were in fact Classes with all the variables
and procedures Shared!

Can you explain the difference?
 
While I could get out the old soapbox, I think I'll just advise readers
to search the ng for this heavily debated topic! But if you don't want
to search too much then the main differences I know of are that modules
are given an automatic project wide import statement (kinda), and can't
be instantiated.

Thanks,

Seth Rowe
 
I'm not sure how they run under the hood. Chances are that they are
just a class.
the main differences I know of are that modules
are given an automatic project wide import statement (kinda),

... which means that you can't control scope. So its better to use a
class and implement it in a way that scope is important. Modules are
often misused. People stick everything in them. I've seen forms
declared in there, database connections, etc.

Implement a class instead. Control access as necessary. Implement a
singleton where relevant. There's always a better way to go.
 
Back
Top