static member inheritance not doing what I want... 
Author Message
 static member inheritance not doing what I want...

Greetings.

If I define a static member variable in a superclass, all classes which
inherit from that class will inherit the *same* instance of that static
(i.e., they all share one value for that static;  they do not maintain
separate values for it).  For example:

public class Super
{
    protected static    MyType        m_foo;

    // do something with m_foo that everyone needs to be able to do

Quote:
}

public class Sub1 : Super
{
    // do something uniquely Sub1-like with m_foo;

Quote:
}

public class Sub2 : Super
{
    // do something uniquely Sub2-like with m_foo;

Quote:
}

If I instantiate both a Sub1 and a Sub2, they both share the same instance
of MyType.

What I'm shooting for is to have a *separate* instance of MyType for each of
the inherited types (i.e., one each for Sub1 and Sub2), but still be able to
work with them up in Super (i.e., have some base methods which work on
MyType as well).

I can think of a million ways to do this from a design patterns perspective,
but they all involve instantiating the object in the Subs and then passing
it (or references, etc.) up to behavior in the Super which requires access
to it.  That strikes me as something of an abstraction weakness, so I find
myself wondering if there's something elegant in the language specification
or a trick up someone's sleeve to achieve this without having to have the
subclasses do any extra work.

Thanks!
/s/ James



Sat, 28 May 2005 07:13:11 GMT  
 static member inheritance not doing what I want...
Sounds like you want some static methods within your MyType class, but for
the MyType member variable in Super to not be static.



Quote:
> Greetings.

> If I define a static member variable in a superclass, all classes which
> inherit from that class will inherit the *same* instance of that static
> (i.e., they all share one value for that static;  they do not maintain
> separate values for it).  For example:

> public class Super
> {
>     protected static    MyType        m_foo;

>     // do something with m_foo that everyone needs to be able to do
> }

> public class Sub1 : Super
> {
>     // do something uniquely Sub1-like with m_foo;
> }

> public class Sub2 : Super
> {
>     // do something uniquely Sub2-like with m_foo;
> }

> If I instantiate both a Sub1 and a Sub2, they both share the same instance
> of MyType.

> What I'm shooting for is to have a *separate* instance of MyType for each
of
> the inherited types (i.e., one each for Sub1 and Sub2), but still be able
to
> work with them up in Super (i.e., have some base methods which work on
> MyType as well).

> I can think of a million ways to do this from a design patterns
perspective,
> but they all involve instantiating the object in the Subs and then passing
> it (or references, etc.) up to behavior in the Super which requires access
> to it.  That strikes me as something of an abstraction weakness, so I find
> myself wondering if there's something elegant in the language
specification
> or a trick up someone's sleeve to achieve this without having to have the
> subclasses do any extra work.

> Thanks!
> /s/ James



Sat, 28 May 2005 07:21:43 GMT  
 static member inheritance not doing what I want...
Mike,

Thanks for the quick reply.  Yes, indeed, that would work.  Problem is that
it would then require all of the Subs to do the same work in instantiating
their types.  Not a problem per se, but that seems ugly if there are 10 subs
or so.  Clearly, just creating a new type and{*filter*} on to it isn't a big
deal, but it just feels wrong some how, since they're all doing the same
thing the same way....

/s/ J


Quote:
> Sounds like you want some static methods within your MyType class, but for
> the MyType member variable in Super to not be static.



> > Greetings.

> > If I define a static member variable in a superclass, all classes which
> > inherit from that class will inherit the *same* instance of that static
> > (i.e., they all share one value for that static;  they do not maintain
> > separate values for it).  For example:

> > public class Super
> > {
> >     protected static    MyType        m_foo;

> >     // do something with m_foo that everyone needs to be able to do
> > }

> > public class Sub1 : Super
> > {
> >     // do something uniquely Sub1-like with m_foo;
> > }

> > public class Sub2 : Super
> > {
> >     // do something uniquely Sub2-like with m_foo;
> > }

> > If I instantiate both a Sub1 and a Sub2, they both share the same
instance
> > of MyType.

> > What I'm shooting for is to have a *separate* instance of MyType for
each
> of
> > the inherited types (i.e., one each for Sub1 and Sub2), but still be
able
> to
> > work with them up in Super (i.e., have some base methods which work on
> > MyType as well).

> > I can think of a million ways to do this from a design patterns
> perspective,
> > but they all involve instantiating the object in the Subs and then
passing
> > it (or references, etc.) up to behavior in the Super which requires
access
> > to it.  That strikes me as something of an abstraction weakness, so I
find
> > myself wondering if there's something elegant in the language
> specification
> > or a trick up someone's sleeve to achieve this without having to have
the
> > subclasses do any extra work.

> > Thanks!
> > /s/ James



Sat, 28 May 2005 07:38:32 GMT  
 static member inheritance not doing what I want...
Not sure if I completely understand but

you could define a static hashtable at class Super level.  That
hashtable would contain a key/value pair.  The value in this case
would be an instance of MyType created whenever class Super (or a
derived type) was instantiated.  

During instantiation, if the static hashtable contains an entry for
the base or derived instance type, then no new MyType would be
created.  But if didn't exist, MyType would be created and added to
the static hashtable.  

Then, whenever you needed to do something Sub2-like, you could use the
class identifier to retrieve the MyType object from the static
hashtable that is specific to that base/derived class' instance of
MyType.

Jonathan Schafer

On Mon, 9 Dec 2002 15:13:11 -0800, "James G. Beldock"

Quote:

>Greetings.

>If I define a static member variable in a superclass, all classes which
>inherit from that class will inherit the *same* instance of that static
>(i.e., they all share one value for that static;  they do not maintain
>separate values for it).  For example:

>public class Super
>{
>    protected static    MyType        m_foo;

>    // do something with m_foo that everyone needs to be able to do
>}

>public class Sub1 : Super
>{
>    // do something uniquely Sub1-like with m_foo;
>}

>public class Sub2 : Super
>{
>    // do something uniquely Sub2-like with m_foo;
>}

>If I instantiate both a Sub1 and a Sub2, they both share the same instance
>of MyType.

>What I'm shooting for is to have a *separate* instance of MyType for each of
>the inherited types (i.e., one each for Sub1 and Sub2), but still be able to
>work with them up in Super (i.e., have some base methods which work on
>MyType as well).

>I can think of a million ways to do this from a design patterns perspective,
>but they all involve instantiating the object in the Subs and then passing
>it (or references, etc.) up to behavior in the Super which requires access
>to it.  That strikes me as something of an abstraction weakness, so I find
>myself wondering if there's something elegant in the language specification
>or a trick up someone's sleeve to achieve this without having to have the
>subclasses do any extra work.

>Thanks!
>/s/ James



Sat, 28 May 2005 08:44:11 GMT  
 static member inheritance not doing what I want...
Hi James:

Interesting question.. I decided to spend 15 minutes to put all this
inheritance stuff straight in my head (and failed :-)

Quote:
> What I'm shooting for is to have a *separate* instance of MyType for each
of
> the inherited types (i.e., one each for Sub1 and Sub2), but still be able
to
> work with them up in Super (i.e., have some base methods which work on
> MyType as well).

another way to put this:

You want to define a base class Super, that has some functions that work on
a static member variable

Then you want to derive a class Sub from it, that does inherit the
functions, but doesn't inherit the static member variable
The functions derived from Super should then work on a static member
variable of Sub instead..

Initialy i thought this was possible (though pretty obscure design wise) by
using the member hiding feature of C#

consider Super:

public class Super
{
    protected static string test = "super";

    public void DoSomethingNormal()
    {
        Console.WriteLine(test);
    }
    public static void DoSomethingSpecific()
    {
        Console.WriteLine(test);
    }
    public void Set(string s)
    {
        test = s;
    }

Quote:
}

and class Sub2:

public class Sub2 : Super
{
    new protected static string test = "sub2";

    public override void DoSomethingSpecific()
    {
        Console.WriteLine( test + " Written by Sub2" );
    }

Quote:
}

Sub2 uses the new keyword to hide the static string test from the Super
class, and this works..

if you call DoSomethingSpecific the output is:  "sub2 Written by Sub2"
exactly as wanted (if it would use the test from Super the output would be
"super Written by Sub2")

even if you cast sub2 to a Super, and then call DoSomethingSpecific() it
still outputs the correct text..

The problem starts when you try to the use DoSomethingNormal() function from
the Super class, which is supposed to be our common functionality. nomatter
what you do, it will output "super", since it uses the test from super and
not from sub2..  our exercise
was pointless.

This is the small test program i made, there were some interesting results
:-)

using System;

namespace InheritanceTest
{
    class test
    {
        [STAThread]
        static void Main()
        {
            Super super = new Super();

            Sub0 sub0 = new Sub0();
            Sub1 sub1 = new Sub1();
            Sub2 sub2 = new Sub2();
            Sub3 sub3 = new Sub3();

            super.DoSomethingSpecific();
            super.DoSomethingNormal();

            Console.WriteLine();

            sub0.DoSomethingNormal();
            sub1.DoSomethingNormal();
            sub2.DoSomethingNormal();
            sub3.DoSomethingNormal();

            Console.WriteLine();

            sub0.DoSomethingSpecific();
            sub1.DoSomethingSpecific();
            sub2.DoSomethingSpecific();
            sub3.DoSomethingSpecific();

            Console.WriteLine();

            ((Super)sub0).DoSomethingNormal();
            ((Super)sub1).DoSomethingNormal();
            ((Super)sub2).DoSomethingNormal();
            ((Super)sub3).DoSomethingNormal();

            Console.WriteLine();

            ((Super)sub0).DoSomethingSpecific();
            ((Super)sub1).DoSomethingSpecific();
            ((Super)sub2).DoSomethingSpecific();
            ((Super)sub3).DoSomethingSpecific();

            Console.WriteLine();

            sub2.Set("Sub2 changed");

            Console.WriteLine();

            sub0.DoSomethingNormal();
            sub1.DoSomethingNormal();
            sub2.DoSomethingNormal();
            sub3.DoSomethingNormal();

            Console.WriteLine();

            sub0.DoSomethingSpecific();
            sub1.DoSomethingSpecific();
            sub2.DoSomethingSpecific();
            sub3.DoSomethingSpecific();

            Console.WriteLine();

            ((Super)sub0).DoSomethingNormal();
            ((Super)sub1).DoSomethingNormal();
            ((Super)sub2).DoSomethingNormal();
            ((Super)sub3).DoSomethingNormal();

            Console.WriteLine();

            ((Super)sub0).DoSomethingSpecific();
            ((Super)sub1).DoSomethingSpecific();
            ((Super)sub2).DoSomethingSpecific();
            ((Super)sub3).DoSomethingSpecific();

            Console.ReadLine();
        }
    }

    public class Super
    {
        protected static string test = "super";

        public void DoSomethingNormal()
        {
            Console.WriteLine(test);
        }

        public virtual void DoSomethingSpecific()
        {
            Console.WriteLine(test);
        }

        public void Set(string s)
        {
            test = s;
        }
    }

    public class Sub0 : Super
    {
        new public void DoSomethingSpecific()
        {
            Console.WriteLine( test + " Written by Sub0" );
        }
    }

    public class Sub1 : Super
    {
        public override void DoSomethingSpecific()
        {
            Console.WriteLine( test + " Written by Sub1" );
        }
    }

    public class Sub2 : Super
    {
        new protected static string test = "sub2";

        public override void DoSomethingSpecific()
        {
            Console.WriteLine( test + " Written by Sub2" );
        }
    }

    public class Sub3 : Super
    {
        new protected static string test = "sub3";

        new public void DoSomethingSpecific()
        {
            Console.WriteLine( test + " Written by Sub3" );
        }
    }

Quote:
}

Actually, now i think about it, there is some sort of way that does not
require code duplication for the common functionality, though it it isn't
fully transparant..

in Super:
1) create a virtual get and set function for your static variable
2) implement all common functionality using those get and set methods

in a Sub
1) define your own class specific static variable
2) override the acces methods to return that variable instead..

this is some example code, it works in all cases i could think of..

using System;

namespace InheritanceTest
{
    class test
    {
        [STAThread]
        static void Main()
        {
            Super super = new Super();

            Sub1 sub1 = new Sub1();
            Sub1 sub1a = new Sub1();
            Sub2 sub2 = new Sub2();
            Sub2 sub2a = new Sub2();

            super.DoCommonStuff();
            sub1.DoCommonStuff();
            sub2.DoCommonStuff();

            Console.WriteLine();

            sub1.SetStaticMember("sub1 changed");

            super.DoCommonStuff();
            sub1.DoCommonStuff();
            sub1a.DoCommonStuff();
            sub2a.DoCommonStuff();
            sub2.DoCommonStuff();

            Console.WriteLine();

            ((Super)sub1).DoCommonStuff();
            ((Super)sub2).DoCommonStuff();

            Console.ReadLine();
        }
    }

    public class Super
    {
        private static string test = "super";

        protected virtual string getValue()
        {
            return test;
        }

        protected virtual void setValue(string s)
        {
            test = s;
        }

        // common functionality
        public void DoCommonStuff()
        {
            Console.WriteLine(getValue() + " Common");
        }

        public void SetStaticMember(string s)
        {
            setValue(s);
        }
    }

    public class Sub1 : Super
    {
        private static string test = "sub1";

        protected override string getValue()
        {
            return test;
        }

        protected override void setValue(string s)
        {
            test = s;
        }
    }

    public class Sub2 : Super
    {
        private static string test = "sub2";

        protected override string getValue()
        {
            return test;
        }

        protected override void setValue(string s)
        {
            test = s;
        }
    }

Quote:
}

ah well, time for bed :-)

Willem



Sun, 29 May 2005 08:31:57 GMT  
 static member inheritance not doing what I want...
is it too much of a bind just to pass the subclass specific static as a
parameter to some common static method (in the base class or wherever?)


Quote:
> Hi James:

> Interesting question.. I decided to spend 15 minutes to put all this
> inheritance stuff straight in my head (and failed :-)

> > What I'm shooting for is to have a *separate* instance of MyType for
each
> of
> > the inherited types (i.e., one each for Sub1 and Sub2), but still be
able
> to
> > work with them up in Super (i.e., have some base methods which work on
> > MyType as well).

> another way to put this:

> You want to define a base class Super, that has some functions that work
on
> a static member variable

> Then you want to derive a class Sub from it, that does inherit the
> functions, but doesn't inherit the static member variable
> The functions derived from Super should then work on a static member
> variable of Sub instead..

> Initialy i thought this was possible (though pretty obscure design wise)
by
> using the member hiding feature of C#

> consider Super:

> public class Super
> {
>     protected static string test = "super";

>     public void DoSomethingNormal()
>     {
>         Console.WriteLine(test);
>     }
>     public static void DoSomethingSpecific()
>     {
>         Console.WriteLine(test);
>     }
>     public void Set(string s)
>     {
>         test = s;
>     }
> }

> and class Sub2:

> public class Sub2 : Super
> {
>     new protected static string test = "sub2";

>     public override void DoSomethingSpecific()
>     {
>         Console.WriteLine( test + " Written by Sub2" );
>     }
> }

> Sub2 uses the new keyword to hide the static string test from the Super
> class, and this works..

> if you call DoSomethingSpecific the output is:  "sub2 Written by Sub2"
> exactly as wanted (if it would use the test from Super the output would be
> "super Written by Sub2")

> even if you cast sub2 to a Super, and then call DoSomethingSpecific() it
> still outputs the correct text..

> The problem starts when you try to the use DoSomethingNormal() function
from
> the Super class, which is supposed to be our common functionality.
nomatter
> what you do, it will output "super", since it uses the test from super and
> not from sub2..  our exercise
> was pointless.

> This is the small test program i made, there were some interesting results
> :-)

> using System;

> namespace InheritanceTest
> {
>     class test
>     {
>         [STAThread]
>         static void Main()
>         {
>             Super super = new Super();

>             Sub0 sub0 = new Sub0();
>             Sub1 sub1 = new Sub1();
>             Sub2 sub2 = new Sub2();
>             Sub3 sub3 = new Sub3();

>             super.DoSomethingSpecific();
>             super.DoSomethingNormal();

>             Console.WriteLine();

>             sub0.DoSomethingNormal();
>             sub1.DoSomethingNormal();
>             sub2.DoSomethingNormal();
>             sub3.DoSomethingNormal();

>             Console.WriteLine();

>             sub0.DoSomethingSpecific();
>             sub1.DoSomethingSpecific();
>             sub2.DoSomethingSpecific();
>             sub3.DoSomethingSpecific();

>             Console.WriteLine();

>             ((Super)sub0).DoSomethingNormal();
>             ((Super)sub1).DoSomethingNormal();
>             ((Super)sub2).DoSomethingNormal();
>             ((Super)sub3).DoSomethingNormal();

>             Console.WriteLine();

>             ((Super)sub0).DoSomethingSpecific();
>             ((Super)sub1).DoSomethingSpecific();
>             ((Super)sub2).DoSomethingSpecific();
>             ((Super)sub3).DoSomethingSpecific();

>             Console.WriteLine();

>             sub2.Set("Sub2 changed");

>             Console.WriteLine();

>             sub0.DoSomethingNormal();
>             sub1.DoSomethingNormal();
>             sub2.DoSomethingNormal();
>             sub3.DoSomethingNormal();

>             Console.WriteLine();

>             sub0.DoSomethingSpecific();
>             sub1.DoSomethingSpecific();
>             sub2.DoSomethingSpecific();
>             sub3.DoSomethingSpecific();

>             Console.WriteLine();

>             ((Super)sub0).DoSomethingNormal();
>             ((Super)sub1).DoSomethingNormal();
>             ((Super)sub2).DoSomethingNormal();
>             ((Super)sub3).DoSomethingNormal();

>             Console.WriteLine();

>             ((Super)sub0).DoSomethingSpecific();
>             ((Super)sub1).DoSomethingSpecific();
>             ((Super)sub2).DoSomethingSpecific();
>             ((Super)sub3).DoSomethingSpecific();

>             Console.ReadLine();
>         }
>     }

>     public class Super
>     {
>         protected static string test = "super";

>         public void DoSomethingNormal()
>         {
>             Console.WriteLine(test);
>         }

>         public virtual void DoSomethingSpecific()
>         {
>             Console.WriteLine(test);
>         }

>         public void Set(string s)
>         {
>             test = s;
>         }
>     }

>     public class Sub0 : Super
>     {
>         new public void DoSomethingSpecific()
>         {
>             Console.WriteLine( test + " Written by Sub0" );
>         }
>     }

>     public class Sub1 : Super
>     {
>         public override void DoSomethingSpecific()
>         {
>             Console.WriteLine( test + " Written by Sub1" );
>         }
>     }

>     public class Sub2 : Super
>     {
>         new protected static string test = "sub2";

>         public override void DoSomethingSpecific()
>         {
>             Console.WriteLine( test + " Written by Sub2" );
>         }
>     }

>     public class Sub3 : Super
>     {
>         new protected static string test = "sub3";

>         new public void DoSomethingSpecific()
>         {
>             Console.WriteLine( test + " Written by Sub3" );
>         }
>     }
> }

> Actually, now i think about it, there is some sort of way that does not
> require code duplication for the common functionality, though it it isn't
> fully transparant..

> in Super:
> 1) create a virtual get and set function for your static variable
> 2) implement all common functionality using those get and set methods

> in a Sub
> 1) define your own class specific static variable
> 2) override the acces methods to return that variable instead..

> this is some example code, it works in all cases i could think of..

> using System;

> namespace InheritanceTest
> {
>     class test
>     {
>         [STAThread]
>         static void Main()
>         {
>             Super super = new Super();

>             Sub1 sub1 = new Sub1();
>             Sub1 sub1a = new Sub1();
>             Sub2 sub2 = new Sub2();
>             Sub2 sub2a = new Sub2();

>             super.DoCommonStuff();
>             sub1.DoCommonStuff();
>             sub2.DoCommonStuff();

>             Console.WriteLine();

>             sub1.SetStaticMember("sub1 changed");

>             super.DoCommonStuff();
>             sub1.DoCommonStuff();
>             sub1a.DoCommonStuff();
>             sub2a.DoCommonStuff();
>             sub2.DoCommonStuff();

>             Console.WriteLine();

>             ((Super)sub1).DoCommonStuff();
>             ((Super)sub2).DoCommonStuff();

>             Console.ReadLine();
>         }
>     }

>     public class Super
>     {
>         private static string test = "super";

>         protected virtual string getValue()
>         {
>             return test;
>         }

>         protected virtual void setValue(string s)
>         {
>             test = s;
>         }

>         // common functionality
>         public void DoCommonStuff()
>         {
>             Console.WriteLine(getValue() + " Common");
>         }

>         public void SetStaticMember(string s)
>         {
>             setValue(s);
>         }
>     }

>     public class Sub1 : Super
>     {
>         private static string test = "sub1";

>         protected override string getValue()
>         {
>             return test;
>         }

>         protected override void setValue(string s)
>         {
>             test = s;
>         }
>     }

>     public class Sub2 : Super
>     {
>         private static string test = "sub2";

>         protected override string getValue()
>         {
>             return test;
>         }

>         protected override void setValue(string s)
>         {
>             test = s;
>         }
>     }
> }

> ah well, time for bed :-)

> Willem



Sun, 29 May 2005 08:49:40 GMT  
 static member inheritance not doing what I want...
Willem,

Wow.  Thanks for all that.  I like your get/set idea, especially for some
specific (and arcane!) reasons in my code.  I'll give it a whirl....

Regards,
/s/ James


Quote:
> Hi James:

> Interesting question.. I decided to spend 15 minutes to put all this
> inheritance stuff straight in my head (and failed :-)

> > What I'm shooting for is to have a *separate* instance of MyType for
each
> of
> > the inherited types (i.e., one each for Sub1 and Sub2), but still be
able
> to
> > work with them up in Super (i.e., have some base methods which work on
> > MyType as well).

> another way to put this:

> You want to define a base class Super, that has some functions that work
on
> a static member variable

> Then you want to derive a class Sub from it, that does inherit the
> functions, but doesn't inherit the static member variable
> The functions derived from Super should then work on a static member
> variable of Sub instead..

> Initialy i thought this was possible (though pretty obscure design wise)
by
> using the member hiding feature of C#

> consider Super:

> public class Super
> {
>     protected static string test = "super";

>     public void DoSomethingNormal()
>     {
>         Console.WriteLine(test);
>     }
>     public static void DoSomethingSpecific()
>     {
>         Console.WriteLine(test);
>     }
>     public void Set(string s)
>     {
>         test = s;
>     }
> }

> and class Sub2:

> public class Sub2 : Super
> {
>     new protected static string test = "sub2";

>     public override void DoSomethingSpecific()
>     {
>         Console.WriteLine( test + " Written by Sub2" );
>     }
> }

> Sub2 uses the new keyword to hide the static string test from the Super
> class, and this works..

> if you call DoSomethingSpecific the output is:  "sub2 Written by Sub2"
> exactly as wanted (if it would use the test from Super the output would be
> "super Written by Sub2")

> even if you cast sub2 to a Super, and then call DoSomethingSpecific() it
> still outputs the correct text..

> The problem starts when you try to the use DoSomethingNormal() function
from
> the Super class, which is supposed to be our common functionality.
nomatter
> what you do, it will output "super", since it uses the test from super and
> not from sub2..  our exercise
> was pointless.

> This is the small test program i made, there were some interesting results
> :-)

> using System;

> namespace InheritanceTest
> {
>     class test
>     {
>         [STAThread]
>         static void Main()
>         {
>             Super super = new Super();

>             Sub0 sub0 = new Sub0();
>             Sub1 sub1 = new Sub1();
>             Sub2 sub2 = new Sub2();
>             Sub3 sub3 = new Sub3();

>             super.DoSomethingSpecific();
>             super.DoSomethingNormal();

>             Console.WriteLine();

>             sub0.DoSomethingNormal();
>             sub1.DoSomethingNormal();
>             sub2.DoSomethingNormal();
>             sub3.DoSomethingNormal();

>             Console.WriteLine();

>             sub0.DoSomethingSpecific();
>             sub1.DoSomethingSpecific();
>             sub2.DoSomethingSpecific();
>             sub3.DoSomethingSpecific();

>             Console.WriteLine();

>             ((Super)sub0).DoSomethingNormal();
>             ((Super)sub1).DoSomethingNormal();
>             ((Super)sub2).DoSomethingNormal();
>             ((Super)sub3).DoSomethingNormal();

>             Console.WriteLine();

>             ((Super)sub0).DoSomethingSpecific();
>             ((Super)sub1).DoSomethingSpecific();
>             ((Super)sub2).DoSomethingSpecific();
>             ((Super)sub3).DoSomethingSpecific();

>             Console.WriteLine();

>             sub2.Set("Sub2 changed");

>             Console.WriteLine();

>             sub0.DoSomethingNormal();
>             sub1.DoSomethingNormal();
>             sub2.DoSomethingNormal();
>             sub3.DoSomethingNormal();

>             Console.WriteLine();

>             sub0.DoSomethingSpecific();
>             sub1.DoSomethingSpecific();
>             sub2.DoSomethingSpecific();
>             sub3.DoSomethingSpecific();

>             Console.WriteLine();

>             ((Super)sub0).DoSomethingNormal();
>             ((Super)sub1).DoSomethingNormal();
>             ((Super)sub2).DoSomethingNormal();
>             ((Super)sub3).DoSomethingNormal();

>             Console.WriteLine();

>             ((Super)sub0).DoSomethingSpecific();
>             ((Super)sub1).DoSomethingSpecific();
>             ((Super)sub2).DoSomethingSpecific();
>             ((Super)sub3).DoSomethingSpecific();

>             Console.ReadLine();
>         }
>     }

>     public class Super
>     {
>         protected static string test = "super";

>         public void DoSomethingNormal()
>         {
>             Console.WriteLine(test);
>         }

>         public virtual void DoSomethingSpecific()
>         {
>             Console.WriteLine(test);
>         }

>         public void Set(string s)
>         {
>             test = s;
>         }
>     }

>     public class Sub0 : Super
>     {
>         new public void DoSomethingSpecific()
>         {
>             Console.WriteLine( test + " Written by Sub0" );
>         }
>     }

>     public class Sub1 : Super
>     {
>         public override void DoSomethingSpecific()
>         {
>             Console.WriteLine( test + " Written by Sub1" );
>         }
>     }

>     public class Sub2 : Super
>     {
>         new protected static string test = "sub2";

>         public override void DoSomethingSpecific()
>         {
>             Console.WriteLine( test + " Written by Sub2" );
>         }
>     }

>     public class Sub3 : Super
>     {
>         new protected static string test = "sub3";

>         new public void DoSomethingSpecific()
>         {
>             Console.WriteLine( test + " Written by Sub3" );
>         }
>     }
> }

> Actually, now i think about it, there is some sort of way that does not
> require code duplication for the common functionality, though it it isn't
> fully transparant..

> in Super:
> 1) create a virtual get and set function for your static variable
> 2) implement all common functionality using those get and set methods

> in a Sub
> 1) define your own class specific static variable
> 2) override the acces methods to return that variable instead..

> this is some example code, it works in all cases i could think of..

> using System;

> namespace InheritanceTest
> {
>     class test
>     {
>         [STAThread]
>         static void Main()
>         {
>             Super super = new Super();

>             Sub1 sub1 = new Sub1();
>             Sub1 sub1a = new Sub1();
>             Sub2 sub2 = new Sub2();
>             Sub2 sub2a = new Sub2();

>             super.DoCommonStuff();
>             sub1.DoCommonStuff();
>             sub2.DoCommonStuff();

>             Console.WriteLine();

>             sub1.SetStaticMember("sub1 changed");

>             super.DoCommonStuff();
>             sub1.DoCommonStuff();
>             sub1a.DoCommonStuff();
>             sub2a.DoCommonStuff();
>             sub2.DoCommonStuff();

>             Console.WriteLine();

>             ((Super)sub1).DoCommonStuff();
>             ((Super)sub2).DoCommonStuff();

>             Console.ReadLine();
>         }
>     }

>     public class Super
>     {
>         private static string test = "super";

>         protected virtual string getValue()
>         {
>             return test;
>         }

>         protected virtual void setValue(string s)
>         {
>             test = s;
>         }

>         // common functionality
>         public void DoCommonStuff()
>         {
>             Console.WriteLine(getValue() + " Common");
>         }

>         public void SetStaticMember(string s)
>         {
>             setValue(s);
>         }
>     }

>     public class Sub1 : Super
>     {
>         private static string test = "sub1";

>         protected override string getValue()
>         {
>             return test;
>         }

>         protected override void setValue(string s)
>         {
>             test = s;
>         }
>     }

>     public class Sub2 : Super
>     {
>         private static string test = "sub2";

>         protected override string getValue()
>         {
>             return test;
>         }

>         protected override void setValue(string s)
>         {
>             test = s;
>         }
>     }
> }

> ah well, time for bed :-)

> Willem



Sun, 29 May 2005 09:07:36 GMT  
 static member inheritance not doing what I want...
Not for me, but i would never use this in code that other might need to
understand anyway :-)
I was just trying to get as close to the result the OP wanted

Willem



Quote:
> is it too much of a bind just to pass the subclass specific static as a
> parameter to some common static method (in the base class or wherever?)



> > Hi James:

> > Interesting question.. I decided to spend 15 minutes to put all this
> > inheritance stuff straight in my head (and failed :-)

> > > What I'm shooting for is to have a *separate* instance of MyType for
> each
> > of
> > > the inherited types (i.e., one each for Sub1 and Sub2), but still be
> able
> > to
> > > work with them up in Super (i.e., have some base methods which work on
> > > MyType as well).

> > another way to put this:

> > You want to define a base class Super, that has some functions that work
> on
> > a static member variable

> > Then you want to derive a class Sub from it, that does inherit the
> > functions, but doesn't inherit the static member variable
> > The functions derived from Super should then work on a static member
> > variable of Sub instead..

> > Initialy i thought this was possible (though pretty obscure design wise)
> by
> > using the member hiding feature of C#

> > consider Super:

> > public class Super
> > {
> >     protected static string test = "super";

> >     public void DoSomethingNormal()
> >     {
> >         Console.WriteLine(test);
> >     }
> >     public static void DoSomethingSpecific()
> >     {
> >         Console.WriteLine(test);
> >     }
> >     public void Set(string s)
> >     {
> >         test = s;
> >     }
> > }

> > and class Sub2:

> > public class Sub2 : Super
> > {
> >     new protected static string test = "sub2";

> >     public override void DoSomethingSpecific()
> >     {
> >         Console.WriteLine( test + " Written by Sub2" );
> >     }
> > }

> > Sub2 uses the new keyword to hide the static string test from the Super
> > class, and this works..

> > if you call DoSomethingSpecific the output is:  "sub2 Written by Sub2"
> > exactly as wanted (if it would use the test from Super the output would
be
> > "super Written by Sub2")

> > even if you cast sub2 to a Super, and then call DoSomethingSpecific() it
> > still outputs the correct text..

> > The problem starts when you try to the use DoSomethingNormal() function
> from
> > the Super class, which is supposed to be our common functionality.
> nomatter
> > what you do, it will output "super", since it uses the test from super
and
> > not from sub2..  our exercise
> > was pointless.

> > This is the small test program i made, there were some interesting
results
> > :-)

> > using System;

> > namespace InheritanceTest
> > {
> >     class test
> >     {
> >         [STAThread]
> >         static void Main()
> >         {
> >             Super super = new Super();

> >             Sub0 sub0 = new Sub0();
> >             Sub1 sub1 = new Sub1();
> >             Sub2 sub2 = new Sub2();
> >             Sub3 sub3 = new Sub3();

> >             super.DoSomethingSpecific();
> >             super.DoSomethingNormal();

> >             Console.WriteLine();

> >             sub0.DoSomethingNormal();
> >             sub1.DoSomethingNormal();
> >             sub2.DoSomethingNormal();
> >             sub3.DoSomethingNormal();

> >             Console.WriteLine();

> >             sub0.DoSomethingSpecific();
> >             sub1.DoSomethingSpecific();
> >             sub2.DoSomethingSpecific();
> >             sub3.DoSomethingSpecific();

> >             Console.WriteLine();

> >             ((Super)sub0).DoSomethingNormal();
> >             ((Super)sub1).DoSomethingNormal();
> >             ((Super)sub2).DoSomethingNormal();
> >             ((Super)sub3).DoSomethingNormal();

> >             Console.WriteLine();

> >             ((Super)sub0).DoSomethingSpecific();
> >             ((Super)sub1).DoSomethingSpecific();
> >             ((Super)sub2).DoSomethingSpecific();
> >             ((Super)sub3).DoSomethingSpecific();

> >             Console.WriteLine();

> >             sub2.Set("Sub2 changed");

> >             Console.WriteLine();

> >             sub0.DoSomethingNormal();
> >             sub1.DoSomethingNormal();
> >             sub2.DoSomethingNormal();
> >             sub3.DoSomethingNormal();

> >             Console.WriteLine();

> >             sub0.DoSomethingSpecific();
> >             sub1.DoSomethingSpecific();
> >             sub2.DoSomethingSpecific();
> >             sub3.DoSomethingSpecific();

> >             Console.WriteLine();

> >             ((Super)sub0).DoSomethingNormal();
> >             ((Super)sub1).DoSomethingNormal();
> >             ((Super)sub2).DoSomethingNormal();
> >             ((Super)sub3).DoSomethingNormal();

> >             Console.WriteLine();

> >             ((Super)sub0).DoSomethingSpecific();
> >             ((Super)sub1).DoSomethingSpecific();
> >             ((Super)sub2).DoSomethingSpecific();
> >             ((Super)sub3).DoSomethingSpecific();

> >             Console.ReadLine();
> >         }
> >     }

> >     public class Super
> >     {
> >         protected static string test = "super";

> >         public void DoSomethingNormal()
> >         {
> >             Console.WriteLine(test);
> >         }

> >         public virtual void DoSomethingSpecific()
> >         {
> >             Console.WriteLine(test);
> >         }

> >         public void Set(string s)
> >         {
> >             test = s;
> >         }
> >     }

> >     public class Sub0 : Super
> >     {
> >         new public void DoSomethingSpecific()
> >         {
> >             Console.WriteLine( test + " Written by Sub0" );
> >         }
> >     }

> >     public class Sub1 : Super
> >     {
> >         public override void DoSomethingSpecific()
> >         {
> >             Console.WriteLine( test + " Written by Sub1" );
> >         }
> >     }

> >     public class Sub2 : Super
> >     {
> >         new protected static string test = "sub2";

> >         public override void DoSomethingSpecific()
> >         {
> >             Console.WriteLine( test + " Written by Sub2" );
> >         }
> >     }

> >     public class Sub3 : Super
> >     {
> >         new protected static string test = "sub3";

> >         new public void DoSomethingSpecific()
> >         {
> >             Console.WriteLine( test + " Written by Sub3" );
> >         }
> >     }
> > }

> > Actually, now i think about it, there is some sort of way that does not
> > require code duplication for the common functionality, though it it
isn't
> > fully transparant..

> > in Super:
> > 1) create a virtual get and set function for your static variable
> > 2) implement all common functionality using those get and set methods

> > in a Sub
> > 1) define your own class specific static variable
> > 2) override the acces methods to return that variable instead..

> > this is some example code, it works in all cases i could think of..

> > using System;

> > namespace InheritanceTest
> > {
> >     class test
> >     {
> >         [STAThread]
> >         static void Main()
> >         {
> >             Super super = new Super();

> >             Sub1 sub1 = new Sub1();
> >             Sub1 sub1a = new Sub1();
> >             Sub2 sub2 = new Sub2();
> >             Sub2 sub2a = new Sub2();

> >             super.DoCommonStuff();
> >             sub1.DoCommonStuff();
> >             sub2.DoCommonStuff();

> >             Console.WriteLine();

> >             sub1.SetStaticMember("sub1 changed");

> >             super.DoCommonStuff();
> >             sub1.DoCommonStuff();
> >             sub1a.DoCommonStuff();
> >             sub2a.DoCommonStuff();
> >             sub2.DoCommonStuff();

> >             Console.WriteLine();

> >             ((Super)sub1).DoCommonStuff();
> >             ((Super)sub2).DoCommonStuff();

> >             Console.ReadLine();
> >         }
> >     }

> >     public class Super
> >     {
> >         private static string test = "super";

> >         protected virtual string getValue()
> >         {
> >             return test;
> >         }

> >         protected virtual void setValue(string s)
> >         {
> >             test = s;
> >         }

> >         // common functionality
> >         public void DoCommonStuff()
> >         {
> >             Console.WriteLine(getValue() + " Common");
> >         }

> >         public void SetStaticMember(string s)
> >         {
> >             setValue(s);
> >         }
> >     }

> >     public class Sub1 : Super
> >     {
> >         private static string test = "sub1";

> >         protected override string getValue()
> >         {
> >             return test;
> >         }

> >         protected override void setValue(string s)
> >         {
> >             test = s;
> >         }
> >     }

> >     public class Sub2 : Super
> >     {
> >         private static string test = "sub2";

> >         protected override string getValue()
> >         {
> >             return test;
> >         }

> >         protected override void setValue(string s)
> >         {
> >             test = s;
> >         }
> >     }
> > }

> > ah well, time for bed :-)

> > Willem



Sun, 29 May 2005 09:54:56 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. static member inheritance

2. AppWizzard not doing static dll option

3. static member of struct in C (NOT C++)

4. Static member not properly defined in a DLL?!

5. Exporting static class members from DLL does not work in VC++5.0

6. static function access member variable and member function

7. Static and non-Static member - Signature

8. struct initialization (with static, const static members)

9. vc wouldn't initialize static members in static libraries

10. IntelliSense (members List) does not show ADO pointer members

11. \n (10) WANTED, \r\n (13,10) NOT WANTED

12. protected members and inheritance

 

 
Powered by phpBB® Forum Software