Using a global variable in .NET

  • Thread starter Thread starter TGF
  • Start date Start date
T

TGF

Hello,

I have two classes....Class A and Class B. I also have a Struct C. Now
I want to have 1 instance of Struct C accesible to both Class A and class B.
Does anyone know the most efficient means to do this using Managed VC++. It
is very simple in unmanaged VC++, but I have no idea how to do this in
MVC++....thanks!

-TGF
 
Well, if I were doing this in C#, I might accomplish this with a readonly
internal static instance of StructC within StructC itself. I'm guessing that
MC++ has a way to do this too.

namespace Test {

internal struct StructC {

static readonly internal StructC Instance = new StructC(10,20);

StructC(int _i,int _j) {
i = _i;
j = _j;
}

public int i;
public int j;
}

// You can access the instance with StructC.Instance
}
 
TGF said:
I have two classes....Class A and Class B. I also have a Struct C. Now
I want to have 1 instance of Struct C accesible to both Class A and class B.
Does anyone know the most efficient means to do this using Managed VC++. It
is very simple in unmanaged VC++, but I have no idea how to do this in
MVC++....thanks!

Is the order in which instances of A and B are created fixed?

If so, and assuming A's are created before B's, I'd define a static member c
(an instance of the class C) of the class A. You can initialize it in the
static constructor for the class A. You can define a public static member
function of A to return c tocallers, e.g. instances of B.

Regards,
Will
 
Back
Top