Discussion Locked

Global variable in C#

0 helpful
3 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

Hi,
working with C#, I created namespace
"First"
{
//with tree public classes
public class A(){}
public class B(){}
public class C(){}
}

How to create a
public string Var;
variable that will be seen (accessable) in all three classes ?

3 replies

by tony hopkinson

Why bother with OO if you are going to do that ?
This is the wrong solution, so what are you trying to achieve ?

by ed woychowsky

I have to agree with Tony, but there is a bizarre solution that probably isn?t worth the overhead. You could always use message queuing (MSMQ) in the following manner:
1. Class A sends ?xyzzy? to a private queue.
2. Class B peeks the queue and sees ?xyzzy?.
3. Class C receives ?xyzzy? and sends ?plugh?.

All you need to do is remember that receive is a destructive read and peek only shows the next item pending in the queue. Unless you?re dealing with a multi-threaded application this would work, but it probably isn?t worth the effort. Also, instead of a string why not create an instance of a String and pass it around?

This discussion is closed to new replies.