static initialisation order?

C

cody

I have a problem with following code, and it involves something similar to
the known issue in c++ programs called "static initialisation order fiasco".
I built a class that follows the "java typesave enum pattern". I made a
generic EnumWrapper class where I wanted all my enums to derive from.
The problem now is: I discovered that when I call EnumWrapper.GetByID()
the first time, the static enum members of the derived class where not
initialized yet.

public class EnumWrapper
{
class Key
{
Type type;
int val;

internal Key(Type type, int val)
{
this.type = type;
this.val = val;
}

public override int GetHashCode()
{
return this.val ^ this.type.GetHashCode();
}

public override bool Equals(object obj)
{
return ((Key)obj).val==this.val && this.type==((Key)obj).type;
}
}

static Hashtable table = new Hashtable();
int val;
string name;

protected EnumWrapper(int val, string name)
{
this.val = val;
this.name = name;
table[new Key(this.GetType(), val)] = this;
}

public static EnumWrapper GetByID(Type type, int val)
{
return (EnumWrapper)table[new Key(type, val)];
}
}

public class Person: EnumWrapper
{
public static Person Kein = new Person(0, "Kein);
public static Person Kunde = new Person(1, "Kunde");
public static Person Lieferant = new Person(2, "Lieferant");

Person(int id, string bez)
: base(id,bez)
{
}
}
 
J

Jon Skeet [C# MVP]

cody said:
I have a problem with following code, and it involves something similar to
the known issue in c++ programs called "static initialisation order fiasco".
I built a class that follows the "java typesave enum pattern". I made a
generic EnumWrapper class where I wanted all my enums to derive from.
The problem now is: I discovered that when I call EnumWrapper.GetByID()
the first time, the static enum members of the derived class where not
initialized yet.

Indeed. You hadn't referenced the Person class, so there was no need
for it to be initialized yet. Basically, you shouldn't expect a class
to be initialized unless something mentions it explicitly.
 
C

cody

Why not use the C# enumerations instead?

Because C# enums are useless for me as they provide no name to display to
the user.
Using ToString() on an enum member yields its identifier but this is of no
use for me.

Additionally one may want a long and a short name for an enum e.g. "NY" and
"New York".
Thats why normal enums are useless in lots of situations.

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
I have a problem with following code, and it involves something similar to
the known issue in c++ programs called "static initialisation order fiasco".
I built a class that follows the "java typesave enum pattern". I made a
generic EnumWrapper class where I wanted all my enums to derive from.
The problem now is: I discovered that when I call EnumWrapper.GetByID()
the first time, the static enum members of the derived class where not
initialized yet.

public class EnumWrapper
{
class Key
{
Type type;
int val;

internal Key(Type type, int val)
{
this.type = type;
this.val = val;
}

public override int GetHashCode()
{
return this.val ^ this.type.GetHashCode();
}

public override bool Equals(object obj)
{
return ((Key)obj).val==this.val && this.type==((Key)obj).type;
}
}

static Hashtable table = new Hashtable();
int val;
string name;

protected EnumWrapper(int val, string name)
{
this.val = val;
this.name = name;
table[new Key(this.GetType(), val)] = this;
}

public static EnumWrapper GetByID(Type type, int val)
{
return (EnumWrapper)table[new Key(type, val)];
}
}

public class Person: EnumWrapper
{
public static Person Kein = new Person(0, "Kein);
public static Person Kunde = new Person(1, "Kunde");
public static Person Lieferant = new Person(2, "Lieferant");

Person(int id, string bez)
: base(id,bez)
{
}
}
 
C

cody

I have a problem with following code, and it involves something similar
to
Indeed. You hadn't referenced the Person class, so there was no need
for it to be initialized yet. Basically, you shouldn't expect a class
to be initialized unless something mentions it explicitly.

You are right, but I was hoping to solve the problem without repeating the
GetByID method in every derived class.
 

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