473,473 Members | 2,309 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Application variables in csharp.

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

May 15 '07 #1
4 6891
On May 15, 12:04 pm, Control Freq <n...@nhthomas.freeserve.co.uk>
wrote:
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

May 15 '07 #2
On 15 May, 12:34, "Jon Skeet [C# MVP]" <s...@pobox.comwrote:
On May 15, 12:04 pm, Control Freq <n...@nhthomas.freeserve.co.uk>
wrote:
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).
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!

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

Jon

May 15 '07 #3
On May 15, 12:57 pm, Control Freq <n...@nhthomas.freeserve.co.uk>
wrote:
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!
Public *static* properties - no instance required.

Jon

May 15 '07 #4
Hi,

"Control Freq" <ni**@nhthomas.freeserve.co.ukwrote in message
news:11*********************@e51g2000hsg.googlegro ups.com...
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
May 15 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Timothy Shih | last post by:
Hi, I am having a freezing issue with my application. My application serves several remotable objects, all of which must be initialized before their use. Furthermore, some of them depend on each...
9
by: Guy | last post by:
I have extended the datetimepicker control to incorporate a ReadOnly property. I have used the new keyword to implement my own version of the value property, so that if readonly == true then it...
2
by: David McCormack | last post by:
I have a small project that I'm writing to help me learn C# and the .Net Framework. This project is a WinForms program with multiple forms that accesses MSDE. I've got most of it done but I've hit...
8
by: dthom | last post by:
Hi, I have a C# service application - and a Win32 / C++ service application running on my system. I need someway to interact between those two - the Win32 application is pure Win32 - so im...
22
by: Nina | last post by:
While user is working on a windows application, at same time he or she launches another instance of the same application. Will this affect the global variables in the first instance of this...
15
by: Sandy | last post by:
My background is web based. I am attempting to write a Windows application and I am finding some simple things difficult. Currently I am trying to find out how to store information session wide....
13
by: Sandeep Singh | last post by:
I am making socket client application in C# how can i get ip address of client who has connected to server
1
by: pagates | last post by:
Hi All, What's the "best" way to get System Environment Variables (VS2003)? Environment.GetEnvironmentVariable("xxx") only gets user Environment variables. Thanks, pagates
12
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor, forward reference, sharing classes between forms....
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.