Visual Basic .NET » Visual Basic .NET General Discussion
Up casting (base to derived) in C# -- AtariPete --


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).

[Submit Comment]Home