Application variables in csharp.

  • Thread starter Thread starter Control Freq
  • Start date Start date
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
 
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
 
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!
 
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
 
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
 
Back
Top