Can Dictionary<> store a static class ref?

M

Mark S.

Hello,

I have a series of changing string IDs that are loaded dynamically a couple
times a minute. I need to associate each ID with a different static class so
later on in the app's lifecycle it knows which static class to use for
processing.

I've tried:
static class MyStaticObject
{
static public bool aValue = false;
}

static class test
{
static void Main(string[] args)
{

Dictionary<String, MyStaticObject> holder = new
Dictionary<String, MyStaticObject>();
}
}

which generates this complie error:
static types cannot be used as generic arguments

I even tried:
Dictionary<String, ref MyStaticObject> holder = new Dictionary<String, ref
MyStaticObject>();

TIA for getting helping approach this problem with a better approach.

M
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Mark said:
Hello,

I have a series of changing string IDs that are loaded dynamically a couple
times a minute. I need to associate each ID with a different static class so
later on in the app's lifecycle it knows which static class to use for
processing.

I've tried:
static class MyStaticObject
{
static public bool aValue = false;
}

static class test
{
static void Main(string[] args)
{

Dictionary<String, MyStaticObject> holder = new
Dictionary<String, MyStaticObject>();
}
}

which generates this complie error:
static types cannot be used as generic arguments

I even tried:
Dictionary<String, ref MyStaticObject> holder = new Dictionary<String, ref
MyStaticObject>();

TIA for getting helping approach this problem with a better approach.

M

You can't store a static class in any type of variable. As there is no
instance of the class, there is no reference to store.

You can get the Type object for the class and store that if you like.

Do you really need to store the static class itself? Can't you make an
enum with values that represent each class?
 
M

Mark S.

Göran,

I don't want store the class itself, I want to store a reference to it. In
theory, I would look up the ID, get the ref value and pass it as a method
arguement which would in turn use it for processing.

Clear as mud?

M
 
D

Doug Semler

Mark S. said:
Göran,

I don't want store the class itself, I want to store a reference to it. In
theory, I would look up the ID, get the ref value and pass it as a method
arguement which would in turn use it for processing.

Clear as mud?


Since you cannot create an instance of a static class, you cannot create a
reference to it...you can't "new" a static class. In addition, your code
that does the "processing" would have to be switching on the various types
to ensure that the correct method/property/fields are used, since you cannot
derive functionality (virtual/override) static functions.

The question becomes, is it more appropriate for you to create base/derived
classes and put the "processing" functions in the classes themselves?



--
Doug Semler, MCPD
a.a. #705, BAAWA. EAC Guardian of the Horn of the IPU (pbuhh).
The answer is 42; DNRC o-
Gur Hfrarg unf orpbzr fb shyy bs penc gurfr qnlf, abbar rira
erpbtavmrf fvzcyr guvatf yvxr ebg13 nalzber. Fnq, vfa'g vg?
 
M

Mark S.

Doug,

Thank you for confirming what I feared would turn out to be the case, I'll
abondon this work around and take a different approach.

M
 

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