Click here to Skip to main content
15,915,062 members
Home / Discussions / C#
   

C#

 
Questionhi Pin
bassam eshteba10-Dec-09 12:01
bassam eshteba10-Dec-09 12:01 
AnswerRe: hi Pin
Luc Pattyn10-Dec-09 12:04
sitebuilderLuc Pattyn10-Dec-09 12:04 
JokeRe: hi Pin
dan!sh 10-Dec-09 17:51
professional dan!sh 10-Dec-09 17:51 
AnswerRe: hi Pin
bassam eshteba10-Dec-09 12:17
bassam eshteba10-Dec-09 12:17 
GeneralRe: hi Pin
Luc Pattyn10-Dec-09 14:10
sitebuilderLuc Pattyn10-Dec-09 14:10 
QuestionUsing html agility pack Pin
Baeltazor10-Dec-09 11:01
Baeltazor10-Dec-09 11:01 
AnswerRe: Using html agility pack Pin
Ravi Bhavnani10-Dec-09 11:20
professionalRavi Bhavnani10-Dec-09 11:20 
QuestionStatic Constructors/Static Initializers Pin
KnyteHawkk10-Dec-09 10:23
KnyteHawkk10-Dec-09 10:23 
Background: C# ASP.Net website
I have a class in my code and about 30 classes derived from it. From a bunch of different points in my code I want to be able to get an instance of one of those derived classes using a string signature. Currently I have a hashtable set up where I put the signature in as the key and an instance of the class as the data. It works great. However, it requires that I have to go to the base class's data initializer and explicitly add the linkage for the signature to the class instance into the hashtable every time I add a new class and this seriously bugs me, especially when I forget to do it and then sadness occurs.

Question: Is there a way, either in the derived classes or as part of the base class, to fire off some code that would add the linkage itself thus removing the need for me to write them out explicitly each time? I had thought that a static constructor would do the job (each derived class has a static constructor that adds the linkage to the hashtable), but apparently in C# static constructors don't fire until you try to instantiate a class the first time (I have tested this and found it to be true), which kind of defeats the purpose for me. In C++ I accomplished this using some wicked nasty global macro stuff at the top of each of the derived class's source file, but the engineer in me tells me there has to be a better way.

Stripped code (the actual classes are hundreds of lines big) for anyone who needs it below. The getClassBySig() function is the sticking point. I need to be able to retrieve a class instance based solely on a string. I don't want to use a giant switch() block to do so, but the current solution isn't a whole lot better from a maintainability standpoint.

public class BaseClass
{
  public BaseClass()
  {
      // constructor code is here                     
  }

  // this is the function used to add the linkages
  public static void addLink(string pSig, BaseClass pInstance)
  {
     // add the entry to the hashtable
  }

  // called when the website loads to initialize the table
  public static void init()
  {
    addLink("link1", new DerivedClass1());
    addLink("link2", new DerivedClass2());
    // etc etc for each derived class, have to add each of these lines by hand

  }

  public static BaseClass getClassBySig(string pSig)
  {
    // return the appropriate class instance from the table based on the sig
  }
}


Thank you very much for any insight you can offer!
AnswerRe: Static Constructors/Static Initializers [modified] Pin
PIEBALDconsult10-Dec-09 10:51
mvePIEBALDconsult10-Dec-09 10:51 
GeneralRe: Static Constructors/Static Initializers Pin
KnyteHawkk10-Dec-09 11:16
KnyteHawkk10-Dec-09 11:16 
GeneralRe: Static Constructors/Static Initializers Pin
PIEBALDconsult10-Dec-09 11:45
mvePIEBALDconsult10-Dec-09 11:45 
GeneralRe: Static Constructors/Static Initializers Pin
KnyteHawkk10-Dec-09 11:46
KnyteHawkk10-Dec-09 11:46 
AnswerRe: Static Constructors/Static Initializers Pin
Ian Shlasko10-Dec-09 11:28
Ian Shlasko10-Dec-09 11:28 
GeneralRe: Static Constructors/Static Initializers Pin
PIEBALDconsult10-Dec-09 11:44
mvePIEBALDconsult10-Dec-09 11:44 
GeneralRe: Static Constructors/Static Initializers Pin
Ian Shlasko10-Dec-09 12:14
Ian Shlasko10-Dec-09 12:14 
GeneralRe: Static Constructors/Static Initializers Pin
PIEBALDconsult10-Dec-09 12:41
mvePIEBALDconsult10-Dec-09 12:41 
AnswerRe: Static Constructors/Static Initializers Pin
PIEBALDconsult11-Dec-09 11:05
mvePIEBALDconsult11-Dec-09 11:05 
Questionzedgraph Pin
jashimu10-Dec-09 7:25
jashimu10-Dec-09 7:25 
AnswerRe: zedgraph Pin
Luc Pattyn10-Dec-09 7:32
sitebuilderLuc Pattyn10-Dec-09 7:32 
GeneralRe: zedgraph Pin
jashimu10-Dec-09 8:27
jashimu10-Dec-09 8:27 
Question<key value=""> collection as a parameter in an interface method</key> Pin
Chiman110-Dec-09 5:51
Chiman110-Dec-09 5:51 
GeneralRe: collection as a parameter in an interface method [modified] Pin
Abhinav S10-Dec-09 6:22
Abhinav S10-Dec-09 6:22 
GeneralRe: collection as a parameter in an interface method Pin
Chiman111-Dec-09 17:34
Chiman111-Dec-09 17:34 
QuestionSend a class object to a webservice Pin
Gktony10-Dec-09 5:32
Gktony10-Dec-09 5:32 
AnswerRe: Send a class object to a webservice Pin
hammerstein0510-Dec-09 5:52
hammerstein0510-Dec-09 5:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.