I have a class which inherits from another and I want to make use of one of the base class functions...but I am running into a problem where calling the base function does not return the member variable from the inherited class as I had hoped.
To illustrate my problem, I have created a simplified example. I expected this example to output "5" but it doesn't :) The solution seems to be that I need to copy the getInt() function from myclass1 into myclass2 too...then it will work, but this seems to defeat the point of inheriting from myclass1 (I wanted to avoid duplicating the function in each class).
class Program
{
static void Main(string[] args)
{
myclass2 myclass = new myclass2();
// Expected to output 5, but it actually outputs 0
Console.WriteLine( myclass.getInt() );
}
}
struct s1
{
public string str;
public int num;
}
struct s2
{
public int num;
public int num2;
}
class myclass1
{
internal s1 mystruct;
internal int getInt()
{
return mystruct.num;
}
}
class myclass2 : myclass1
{
internal new s2 mystruct;
internal myclass2()
{
mystruct.num = 5;
}
}