Where i can define global variable?

T

Tark Siala

hi
i worked with VB6 then i changed to C#.
i want define Variable thats i can accessed from any Form or Class in my
project.
in VB6 you can do that by define as "Global" in Module, but in C# i can't
find Global for all forms in project.
 
J

Jon Skeet [C# MVP]

i worked with VB6 then i changed to C#.
i want define Variable thats i can accessed from any Form or Class in my
project.
in VB6 you can do that by define as "Global" in Module, but in C# i can't
find Global for all forms in project.

You can use a "public static" variable, or "public const" where
appropriate.
You'll need to explicitly state which type contains the variable from,
though. For instance, in SomeForm.cs you might say

label.Text = TypeContainingGlobals.SomeGlobalName;

Jon
 
R

rossum

hi
i worked with VB6 then i changed to C#.
i want define Variable thats i can accessed from any Form or Class in my
project.
in VB6 you can do that by define as "Global" in Module, but in C# i can't
find Global for all forms in project.
Try:

public static class MyGlobals {
public static int myInt = 42;
public static string myString = "Armadillo";
}

rossum
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top