Creating a static class from non static Framework classes 
Author Message
 Creating a static class from non static Framework classes

Hi,
Can anyone help - this is either a lack of knowledge on my part or
it's really
not possible.

I simply want to create a "Global Variables" class that builds some
key/value pairs once per run and is then accessible to all other
classes. The System.Collections.DictionaryBase class has the features
I need so I want to inherit from it...

public sealed class clsGlobalData : System.Collections.DictionaryBase
static clsGlobalData() //static constructor
{
Dictionary.Clear(); // COMPILE ERROR CS0120
clsGlobalData.Add (new DictionaryEntry("Key","Value"));
etc...

Quote:
}

The error is "An object reference is required for the nonstatic field,
method, or property 'System.Collections.DictionaryBase.Dictionary'"

If I'd written the DictionaryBase class I could make it static too.

I've played around with creating an explicit object reference but it's
messy.

Any help would be invaluable.

Thanks!



Wed, 06 Apr 2005 02:17:17 GMT  
 Creating a static class from non static Framework classes
The idea is, there is no Dictionary property to speak of if you don't have
an instance of the class, becase only an instance can have a Dictionary.
What you need is a singleton class:

public sealed class clsGlobalData : System.Collections.DictionaryBase
private clsGlobalData myData;
public static clsGlobalData GetInstance()
{
if (myData == null)
{
  myData = new clsGlobalData();

Quote:
}

return myData

Quote:
}

This ensure that only one instance of the object gets created. Additionally,
you add whatever methods you need to, add a constructor (private or
protected), etc.  Instead of a method, you can turn GetInstance into a
property - in fact it may work better that way.

You should not have a public constructor, ensuring that the only way your
users can get an instance of this class is to go through your property or
method to get one.

Be sure you make access to this class thread safe if multiple threads will
be accessing it at the same time.


Quote:
> Hi,
> Can anyone help - this is either a lack of knowledge on my part or
> it's really
> not possible.

> I simply want to create a "Global Variables" class that builds some
> key/value pairs once per run and is then accessible to all other
> classes. The System.Collections.DictionaryBase class has the features
> I need so I want to inherit from it...

> public sealed class clsGlobalData : System.Collections.DictionaryBase
> static clsGlobalData() //static constructor
> {
> Dictionary.Clear(); // COMPILE ERROR CS0120
> clsGlobalData.Add (new DictionaryEntry("Key","Value"));
> etc...
> }

> The error is "An object reference is required for the nonstatic field,
> method, or property 'System.Collections.DictionaryBase.Dictionary'"

> If I'd written the DictionaryBase class I could make it static too.

> I've played around with creating an explicit object reference but it's
> messy.

> Any help would be invaluable.

> Thanks!



Wed, 06 Apr 2005 02:33:01 GMT  
 Creating a static class from non static Framework classes

Quote:

> Hi,
> Can anyone help - this is either a lack of knowledge on my part or
> it's really
> not possible.

> I simply want to create a "Global Variables" class that builds some
> key/value pairs once per run and is then accessible to all other
> classes. The System.Collections.DictionaryBase class has the features
> I need so I want to inherit from it...

That's not necessarily the way to think about it - and in this case I
would suggest you *don't* actually inherit from anything (other than
object implicitly). Using composition is often preferrable to using
inheritance. Just have a private static Dictionary field.

--

http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too



Wed, 06 Apr 2005 06:42:57 GMT  
 Creating a static class from non static Framework classes

Quote:


>> Hi,
>> Can anyone help - this is either a lack of knowledge on my part or
>> it's really
>> not possible.

>> I simply want to create a "Global Variables" class that builds some
>> key/value pairs once per run and is then accessible to all other
>> classes. The System.Collections.DictionaryBase class has the features
>> I need so I want to inherit from it...

>That's not necessarily the way to think about it - and in this case I
>would suggest you *don't* actually inherit from anything (other than
>object implicitly). Using composition is often preferrable to using
>inheritance. Just have a private static Dictionary field.

As Jon has suggested don't inherit, just contain.

You may want to check out
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnbd...
for how to create a "global" using the singleton pattern

Jonathan Wilson,
AS/400 Consultant. (UK)
+44 (0)115 9192648
ASP/ASPX/.Net coder



Wed, 06 Apr 2005 18:25:25 GMT  
 Creating a static class from non static Framework classes
Hi,
Thanks for the time you all put into this!

Just to clarify - I should create a base class with private static
members
for a dictionary collection and dictionaryentry objects? I assume this
will remove the "thread safe" issue too?

Also, I don't have to check for multiple instantiations or anything?
The "static" members will take care of that?

Thanks again!

<snip>
 Just have a private static Dictionary field.



Wed, 06 Apr 2005 20:00:43 GMT  
 Creating a static class from non static Framework classes
What do you mean creating an explicit object reference is too messy ? If
it is a non-static member of a class as the compiler says then you must
create an object for the member to be accessed from your program. It is
the easiest of errors to solve so, I guess your problem must be
something else. Please explain it in detail.

with regards,

J.V.Ravichandran

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!



Wed, 06 Apr 2005 23:32:52 GMT  
 Creating a static class from non static Framework classes

Quote:

> Thanks for the time you all put into this!
> Just to clarify - I should create a base class with private static
> members
> for a dictionary collection and dictionaryentry objects?

Why would you want DictionaryEntry members? You only need the Dictionary
itself, I'd have thought.

Quote:
> I assume this
> will remove the "thread safe" issue too?

That depends on what you want to do - if you only want read access after
set-up, it'll all be fine. If you want to be able to support multiple
threads writing and reading at the same time, you'll need to put some
more work in - but .NET provides some helpful classes.

Quote:
> Also, I don't have to check for multiple instantiations or anything?
> The "static" members will take care of that?

You need never instantiate your class at all - and indeed you might want
to make the constructor private so it *can't* be constructed.

--
Jon Skeet

If replying to the group, please do not mail me at the same time



Thu, 07 Apr 2005 01:39:20 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Making timer callback non static in class

2. non-static const member ... class without a constructor

3. how to call a non-static member function of a class from a static member function of the class?

4. class hierarchy for classes with only static methods

5. does static class member change class size?

6. Class object as static member of another class

7. create static lib wth mfc classes

8. Static and non-Static member - Signature

9. Pointers to non-static vs static functions

10. One Problem in Calling Static function from Non static function

11. static versus non static returnvalues

12. Static array containing non static objects and functions

 

 
Powered by phpBB® Forum Software