Are you OK with the following:?
class GenericBase<T>
{
    public static int x;
}

class GenericClass1 : GenericBase<int>
{
}

class GenericClass2 : GenericBase<decimal>
{
}

class GenericClass3: GenericBase<int>
{
}
//...
GenericClass1.x = 5;
GenericClass2.x = 10;
GenericClass3.x = 15;

String text;
text.Format("{0} {1} {2}", new object[] {GenericClass1.x, GenericClass2.x,
GenericClass3.x});
Debug.WriteLine(text);

//eof

...which would output "15 10 15" not "5 10 15"?

On Mon, 20 Feb 2006 17:30:04 +1300, Dean Cleaver
<[EMAIL PROTECTED]> wrote:

>Peter,
>
>Actually, now that you mention it I will see what I want. Basically, in
>almost every class I have I have 2 static datasets containing a list of
>all of the items in the database for that class - especially the likes
>of a "Country" table where countries are not added regularly if ever, I
>keep an in-memory copy of them for quick population of a combo box for
>example. Saves on round-tripping to the db when on a VPN from a remote
>office.
>
>So what I would be doing is GenericBase<x> and then GenericBase<y> etc
>where every class will have a different generic type, which from what
>you are saying will in fact give me my desired results, and a quick test
>has proven that:
>
>
>class GenericBase<T>
>{
>    public static int x;
>}
>
>class GenericClass1 : GenericBase<int>
>{
>}
>
>class GenericClass2 : GenericBase<decimal>
>{
>}
>
>And then ran this:
>
>GenericClass1.x = 5;
>GenericClass2.x = 10;
>MessageBox.Show(GenericClass1.x.ToString());
>
>Returns 5 - meaning that I can define a Generic Base with static
>members, and then define 150 classes from that each based on a different
>type, and I will in effect have 150 individual sets of static members,
>not 1 static member like I would have had with a normal non-generic base
>class. I just made the mistake of creating 2 classes of
>GenericBase<int>, which will share the static members.
>
>Much appreciated - I'd been looking at the code trying all sorts, but
>never thought to change the type.
>
>Cheers,
>Dino
>
>-----Original Message-----
>From: Discussion of advanced .NET topics.
>[mailto:[EMAIL PROTECTED] On Behalf Of Peter Ritchie
>Sent: Monday, 20 February 2006 16:54
>To: [email protected]
>Subject: Re: [ADVANCED-DOTNET] Statics on Generic classes
>
>Statics in generics don't operate any differently than non-generic
>classes.  They're basically globals that are scoped within a class.
>
>Generics are also not like unmanaged C++ templates; their body is not
>copied (inlined) for each use.
>
>In your example, x is a member of GenericBase<int>.  If you changed your
>declaration of GenericClass2 to derive from GenericBase<Decimal> (or any
>other type except int) then you'd see the results you expected, but not
>what you want.
>
>If you're expecting a class declaration deriving from a generic to
>operate like an instance (i.e. each class declaration have its own copy
>of derived
>statics) then you're out of luck.
>
>What were you hoping to accomplish?  In case there's another way of
>doing what you want.
>
>On Mon, 20 Feb 2006 15:43:36 +1300, Dean Cleaver
><[EMAIL PROTECTED]> wrote:
>
>>I just tried a test like this:
>>
>>class GenericBase<T>
>>{
>>    public static int x;
>>}
>>
>>class GenericClass1 : GenericBase<int>
>>{
>>}
>>
>>class GenericClass2 : GenericBase<int>
>>{
>>}
>>
>>And then ran this:
>>
>>GenericClass1.x = 5;
>>GenericClass2.x = 10;
>>MessageBox.Show(GenericClass1.x.ToString());
>>
>>To my disappointment, it displayed 10 not 5 as I had hoped - basically
>>means that any statics on a Generic base are common to all derivations
>>of that Generic class, not to each derived class - or is there another
>>way to effect what I am trying to do?
>
>===================================
>This list is hosted by DevelopMentor(r)  http://www.develop.com
>
>View archives and manage your subscription(s) at
>http://discuss.develop.com
>
>===================================
>This list is hosted by DevelopMentorĀ®  http://www.develop.com
>
>View archives and manage your subscription(s) at
http://discuss.develop.com

===================================
This list is hosted by DevelopMentorĀ®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to