Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / September 2007

Tip: Looking for answers? Try searching our database.

Can Dictionary<> store a static class  ref?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Mark S. - 23 Sep 2007 16:36 GMT
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
Göran Andersson - 23 Sep 2007 17:06 GMT
> Hello,
>
[quoted text clipped - 29 lines]
>
> 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?

Signature

Göran Andersson
_____
http://www.guffa.com

Mark S. - 23 Sep 2007 18:11 GMT
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
Doug Semler - 23 Sep 2007 18:33 GMT
> Göran,
>
[quoted text clipped - 3 lines]
>
> 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?

Signature

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?

Mark S. - 23 Sep 2007 18:47 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.