Application variables in csharp.

C

Control Freq

Hi,
Still new to csharp. I am coming from a C++ background.

In C++ I would create a few top level variables in the application
class. These are effectively global variables which can be accessed
throughout the application because the application object is known.

What is the csharp equivalent of this practice?
I can't seem to add variables to the "public class Program" class and
get access to them from other files.

I am probably missing something obvious.

Please help.

Regards
 
J

Jon Skeet [C# MVP]

Still new to csharp. I am coming from a C++ background.

In C++ I would create a few top level variables in the application
class. These are effectively global variables which can be accessed
throughout the application because the application object is known.

What is the csharp equivalent of this practice?
I can't seem to add variables to the "public class Program" class and
get access to them from other files.

I am probably missing something obvious.

Basically you want public static fields (or preferably properties).

An alternative to this is a singleton:
http://pobox.com/~skeet/csharp/singleton.html

Jon
 
C

Control Freq

Basically you want public static fields (or preferably properties).

I don't get it.
I could use public properties if I could only get hold of the
application object!
So, I have a Program class. And in it are two objects of class CFoo
and CBar . A method in CFoo wants to call some method in CBar . I
don't want to pass a pointer to the CBar into CFoo methods. The top
level application should be able to give me some reference to the
objects.

Still vague.

Help!
 
J

Jon Skeet [C# MVP]

I don't get it.
I could use public properties if I could only get hold of the
application object!

Public *static* properties - no instance required.

Jon
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

I don't get it.
I could use public properties if I could only get hold of the
application object!

What you call the application objext?
So, I have a Program class. And in it are two objects of class CFoo
and CBar . A method in CFoo wants to call some method in CBar . I
don't want to pass a pointer to the CBar into CFoo methods. The top
level application should be able to give me some reference to the
objects.

Still vague.


you could declare CBar like this:

public class CBar
{
public static void Method(){}
}

so now your CFoo can call it like:

CBar.Method();

so in other word you do not need to hold an instance of CBar to call Method
 

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