How to store a reference to a global variable?

G

Gianco

Hello people

I'm trying to translate a C++ class to C#, it relates some global
variables (the application parameters) to the windows registry and
generically manages the edit of this parameters in a form, after the
edit the class must update the global variables and the registry
voices

This class stores internally the pointers to the global variables
(parameters) and for each variable the reference to the registry
voices. I would that this new values are visible also outside the
class, with C++ is very easy but in C#.... ???

with C++ i could have (is just an example, not the class previously
descripted)

int iMyGlobalInteger = 0;

MyClass *TreatGlobalInteger = new MyClass(&iMyGlobalInteger);

// This change iMyGlobalInteger to 1
TreatGlobalInteger->AddOneToTheInternalValue();

// The program does other stuffs

// This change iMyGlobalInteger to 2
TreatGlobalInteger->AddOneToTheInternalValue();

// The program does other stuffs

// This change iMyGlobalInteger to 3
TreatGlobalInteger->AddOneToTheInternalValue();

// The program does other stuffs

// Now the new value of iMyGlobalInteger (3) is shown...
cout << iMyGlobalInteger;

I know that the addresses of C# variables are not fixed so they cannot
be stored so my question is, how can I store any fixed reference to
the variables? Can I access to the global variables as "resources". In
this case I could store, to have a reference, the variable name in a
string instead of it address

Any good idea is welcome!!!

Thank you in advance
Gianco
 
I

Ibai Peña

You could use a Singleton object with the global variables as properties.
So, you would be sharing the properites values where you create a instance
of the singleton object.

Regards,
 
G

Gianco

I should implement this class in C#, I suspect that the SingleTon
objects are available only in C++, could it be?

Thank you
Gianco
 
C

chris-s

Will declaring your global variables as 'static' work for you? Sounds like
it will.

'Singletons' are usually applied to classes rather than individual
vairables, tho I guess you could have a 'singleton' class of global
variables.

Chris
 

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