M
Mark Broadbent
Hi all. I've come across one of those things that doesn't work as I'd
expect. When inheriting a class, I have read that I can use the "new"
keyword to hide an inherited member and create a new implementation of
it -it is supposed to be possible to change the access level of the new
member. However what I have found is that if the member level is changed,
what we effectively get is a new member with a different access level (good)
AND the old member is inherited with its access level-see code below. What I
expected to see is that the old member SHOULD remain hidden in the object
instance of the inheriting class.
using System;
class inheritThisClass{
public string myString="from inheritThisClass";
public void myMethod(){
Console.WriteLine("inside class inheritThisClass");
}
public string myProperty{
set{
value="from inheritThisClass";
}
}
}
class useClass:inheritThisClass{
new private string myString="from useClass";
public string anotherString;
//constr to use private string
public useClass(){
anotherString=myString;
}
}
class UseClass{
public static void Main(){
useClass obj=new useClass();
Console.WriteLine("myString field="+obj.myString);
Console.WriteLine("anotherString field(set to useClass'
myString="+obj.anotherString);
Console.ReadLine();
}
}
expect. When inheriting a class, I have read that I can use the "new"
keyword to hide an inherited member and create a new implementation of
it -it is supposed to be possible to change the access level of the new
member. However what I have found is that if the member level is changed,
what we effectively get is a new member with a different access level (good)
AND the old member is inherited with its access level-see code below. What I
expected to see is that the old member SHOULD remain hidden in the object
instance of the inheriting class.
using System;
class inheritThisClass{
public string myString="from inheritThisClass";
public void myMethod(){
Console.WriteLine("inside class inheritThisClass");
}
public string myProperty{
set{
value="from inheritThisClass";
}
}
}
class useClass:inheritThisClass{
new private string myString="from useClass";
public string anotherString;
//constr to use private string
public useClass(){
anotherString=myString;
}
}
class UseClass{
public static void Main(){
useClass obj=new useClass();
Console.WriteLine("myString field="+obj.myString);
Console.WriteLine("anotherString field(set to useClass'
myString="+obj.anotherString);
Console.ReadLine();
}
}