The keyword new is required on 'B.IsValid' because it hides inherited member 'A.IsValid'

  • Thread starter Thread starter Paul Tomlinson
  • Start date Start date
P

Paul Tomlinson

The keyword new is required on 'B.IsValid' because it hides inherited member
'A.IsValid'
All I have a couple of classes which resemble (sort of!) this:

public class A
{
public bool bIsValid = true;

public bool IsValid
{
get{ return bIsValid; }
}
}


public class B : A
{
public bool bIsValid = false

public bool IsValid
{
get{ return bIsValid; }
}
}

Now whenever I call IsValid on an object of type A I always want to get
"true" returned, and likewise for an object of type B I always want to get
"false" returned.

How can I acheive this and get rid of this warning at the same time?

Thanks
MA
 
Paul Tomlinson said:
The keyword new is required on 'B.IsValid' because it hides inherited
member 'A.IsValid'
All I have a couple of classes which resemble (sort of!) this:

public class A
{
public bool bIsValid = true;

public bool IsValid
{
get{ return bIsValid; }
}
}


public class B : A
{
public bool bIsValid = false

public bool IsValid
{
get{ return bIsValid; }
}
}

Now whenever I call IsValid on an object of type A I always want to get
"true" returned, and likewise for an object of type B I always want to get
"false" returned.

How can I acheive this and get rid of this warning at the same time?

Thanks
MA

This is basic inheritence, so you need to "override" the IsValid method
defined in class A in class B if you want it to act differently in the
inherited class, e.g:

public class A
{
public bool IsValid
{
get { return true; }
}
}

public class B : A
{
public override bool IsValid
{
get { return false; }
}
}
 
Jako Menkveld said:
This is basic inheritence, so you need to "override" the IsValid method
defined in class A in class B if you want it to act differently in the
inherited class, e.g:

public class A
{
public bool IsValid
{
get { return true; }
}
}

public class B : A
{
public override bool IsValid
{
get { return false; }
}
}

Oh, just remembered, you have to mark A.IsValid as virtual, otherwise you
will get a compiler error:

e.g. In class A
public virtual bool IsValid
{
....
}

The virtual keyword tells the compiler that the method can be overriden by
subclasses.
 
Back
Top