List Info

Thread: C-Sharp (C#) Group: Up casting (base to derived) in C#




C-Sharp (C#) Group: Up casting (base to derived) in C#
user name
2006-07-26 15:55:24
Hey All,

I have a C# question for you regarding up casting (base to
derived).

I was wondering about the most elegant way (readable, less
code)  to
cast from a base type to its derived type. Please consider
the
following two classes:

    class Base {
        private int m_valA;
        private int m_valB;

        public Base() { }

        public Base(int a, int b){
            m_valA = a;
            m_valB = b;
        }

        public int ValA
        {
            get { return m_valA; }
            set { m_valA = value; }
        }

        public int ValB
        {
            get { return m_valB; }
            set { m_valB = value; }
        }
    }//end class

    class Derived : Base
    {
        public int ValAPlusValB
        {
            get { return base.ValA + base.ValB; }
        }
    }//end class

Now consider the scenario where I have an instance of Base
(someBase)
and could like to up cast it to a Derived instance
(someDerived).


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "C-Sharp (C#)" group.
To post to this group, send email to C_Sharpgooglegroups.com
To unsubscribe from this group, send email to
C_Sharp-unsubscribegooglegroups.com
For more options, visit this group at http://groups.
google.com/group/C_Sharp
-~----------~----~----~----~------~----~------~--~---

C-Sharp (C#) Group: Re: Up casting (base to derived) in C#
user name
2006-07-31 19:32:15
Pete,
     I would have responded sooner, but I've been hoping
that someone
else would, to be perfectly honest. I would have to say that
my biggest
question in this entire scenario is this: I cannot fathom
the latter
scenario you have mentioned.

When will a base class ever need to be cast into a phantom
child class?
You would only need to cast when the child needs to be
compared to what
'else' it is. If you stop on a lower level, then the base
is never
anything 'else'.

I mean no disrespect in this, as I'm sure I have just
missed something
overly obvious and will be smacking myself for it later, but
the best
way I can sum this up is to say that a cub came from a bear,
but never
has a cub turned around and created a bear in return.

Please elaborate your position if I somehow went brain-dead
(as I often
do) and missed it. Thanks much in advance.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "C-Sharp (C#)" group.
To post to this group, send email to C_Sharpgooglegroups.com
To unsubscribe from this group, send email to
C_Sharp-unsubscribegooglegroups.com
For more options, visit this group at http://groups.
google.com/group/C_Sharp
-~----------~----~----~----~------~----~------~--~---

C-Sharp (C#) Group: Re: Up casting (base to derived) in C#
user name
2006-07-31 22:08:38
Lance, It is an odd scenario to run in to but when you get in to some of the more complex abstraction scenarios it is perfectly valid. A good example of this is with virtual members. If you don't cast to the Child type you can completely bypass the Childs implementation and just call the Base implementation instead.

As for the best method to do this:

ChildClass Child = Base as ChildClass;
if (Child != null)
{
      //Do stuff
}
else
{
   ; //Throw
}

You could do ((ChildClass)Base).Method but you would need to handle an Exception if it can't perform the cast which is much less efficient then just using an "as".

Liam

On 7/31/06, Lance May <gmail.com">lancemaygmail.com> wrote:

Pete,
&nbsp; &nbsp;  I would have responded sooner, but I've been hoping that someone
else would, to be perfectly honest. I would have to say that my biggest
question in this entire scenario is this: I cannot fathom the latter
scenario you have mentioned.

When will a base class ever need to be cast into a phantom child class?
You would only need to cast when the child needs to be compared to what
'else' it is. If you stop on a lower level, then the base is never
anything 'else'.

I mean no disrespect in this, as I'm sure I have just missed something
overly obvious and will be smacking myself for it later, but the best
way I can sum this up is to say that a cub came from a bear, but never
has a cub turned around and created a bear in return.

Please elaborate your position if I somehow went brain-dead (as I often
do) and missed it. Thanks much in advance.



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "C-Sharp (C#)" group.
To post to this group, send email to C_Sharpgooglegroups.com
To unsubscribe from this group, send email to C_Sharp-unsubscribegooglegroups.com
For more options, visit this group at http://groups.google.com/group/C_Sharp
-~----------~----~----~----~------~----~------~--~---

[1-3]

about | contact  Other archives ( Real Estate discussion Medical topics )