We're on the same page, at the Foo<MyTypedDataSet> level.

Thanks,

Shawn Wildermuth
http://adoguy.com
C# MVP, MCSD.NET, Author and Speaker


-----Original Message-----
From: Discussion of advanced .NET topics.
[mailto:[EMAIL PROTECTED] On Behalf Of Dean Cleaver
Sent: Monday, February 20, 2006 1:45 AM
To: [email protected]
Subject: Re: [ADVANCED-DOTNET] Statics on Generic classes

Shawn,

When you say "keep static data about the schema at the type level" did you
mean at the Foo<> level, or the Foo<MyTypedDataSet> level? The CLR currently
keeps it at the Foo<MyTypedDataSet> which is what I want, and I think what
you mean.

I'm looking at cutting 1000's of lines of code from my projects, because I
have lots of code in there that's identical, but static to each class so
couldn't be inherited from a base - but you can do that with a generic base.

Dino

-----Original Message-----
From: Discussion of advanced .NET topics.
[mailto:[EMAIL PROTECTED] On Behalf Of Shawn Wildermuth
Sent: Monday, 20 February 2006 19:18
To: [email protected]
Subject: Re: [ADVANCED-DOTNET] Statics on Generic classes

This question got me thinking a lot about overhead and statics.  I have to
say I think I like this implementation (where Foo<int> and Foo<string> are
two different types).  If I were to write a wrapper that did
Foo<MyTypedDataSet> and keep static data about the schema at the type level,
it might cause a lot of static data, but it might perform really well
instead of non-shared schema like we have today.



Thanks,

Shawn Wildermuth
http://adoguy.com
C# MVP, MCSD.NET, Author and Speaker


-----Original Message-----
From: Discussion of advanced .NET topics.
[mailto:[EMAIL PROTECTED] On Behalf Of Dean Cleaver
Sent: Sunday, February 19, 2006 11:30 PM
To: [email protected]
Subject: Re: [ADVANCED-DOTNET] Statics on Generic classes

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(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