Can Dictionary<> store a static class ref?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mark S.

    #1

    Can Dictionary<> store a static class ref?

    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<Stri ng, MyStaticObjecth older = new
    Dictionary<Stri ng, MyStaticObject> ();
    }
    }

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

    I even tried:
    Dictionary<Stri ng, ref MyStaticObjecth older = new Dictionary<Stri ng, ref
    MyStaticObject> ();

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

    M



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

    #2
    Re: Can Dictionary&lt;& gt; store a static class ref?

    Mark S. wrote:
    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<Stri ng, MyStaticObjecth older = new
    Dictionary<Stri ng, MyStaticObject> ();
    }
    }
    >
    which generates this complie error:
    static types cannot be used as generic arguments
    >
    I even tried:
    Dictionary<Stri ng, ref MyStaticObjecth older = new Dictionary<Stri ng, 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?

    --
    Göran Andersson
    _____
    Göran Anderssons privata hemsida.

    Comment

    • Mark S.

      #3
      Re: Can Dictionary&lt;& gt; store a static class ref?

      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


      Comment

      • Doug Semler

        #4
        Re: Can Dictionary&lt;& gt; store a static class ref?

        "Mark S." <marks@yahoo.co mwrote in message
        news:eQ%23MPTg$ HHA.536@TK2MSFT NGP06.phx.gbl.. .
        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?

        Comment

        • Mark S.

          #5
          Re: Can Dictionary&lt;& gt; store a static class ref?

          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


          Comment

          Working...