how to populate a static hastable?

  • Thread starter Thread starter Girish
  • Start date Start date
G

Girish

How do i populate a static hastable without the need to instantiate the
class? any ideas? Im confused.

class VAErrorMap
{
static Hasttable _ErrorMap = new Hashtable();

public VAErrorMap()
{
//populate has here.. but dont want to!
_ErrorMap.Add(1, "error in user input");
_ErrorMap.Add(2, "error in input file");
//etc etc
}
}

Im trying to figure out a way to populate the variable in class scope so i
dont have to instantiate it. The other way would be a singleton - but i dont
want to do this as a sigleton. I cant think of another way.. maybe its
simple and Im just overlooking something.

Thanks,
Girish
 
class VAErrorMap
{
static Hasttable _ErrorMap = new Hashtable();

static VAErrorMap() // static constructor
{
_ErrorMap.Add(1, "error in user input");
_ErrorMap.Add(2, "error in input file");
//etc etc
}
}

HTH;
Eric Cadwell
http://www.origincontrols.com
 
cool! are static constructors a new feature in c# or do all oo programming
languages have it?

VB, Java, OOPerl, C++?

thanks,
girish
 
Hi Girish,

Thank you for posting in the community!

Yes, static construcotr gives you a chance to initialize the CLASS not a
class instance.
Many of the .Net languages have implemented this feature, C# and Managed
C++ both have the Static Construcotr, please refer to:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/
vclrfStaticConstructors.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmxspec/ht
ml/vcmanagedextensionsspec_19.asp

In VB.net, static is represented as "Shared" key word, it has "Shared
Constructor", which is similiar as the Static Construcotr, please refer to:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbls7/html/
vblrfvbspec7_2_2.asp

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Back
Top