473,569 Members | 2,799 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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
Nov 16 '05 #1
2 6079
"Paul Tomlinson" <ru************ @hotmail.com> wrote in message
news:eD******** ******@TK2MSFTN GP15.phx.gbl...
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; }
}
}
Nov 16 '05 #2
"Jako Menkveld" <ja***********@ envalue.ch> wrote in message
news:uF******** *****@TK2MSFTNG P10.phx.gbl...
"Paul Tomlinson" <ru************ @hotmail.com> wrote in message
news:eD******** ******@TK2MSFTN GP15.phx.gbl...
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; }
}
}


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.
Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1183
by: Daniel Barna | last post by:
Hi I am a bit confused. I thought always that if a class inherits from another (public), then all of its base class's public member function are also visible in this class - even if it declares a function with the same name, but different parameters. However, when I try to compile the example below, the compiler claims that there is no...
4
1855
by: Lionel B | last post by:
Greetings, The following code: <code> template<typename T> class A { protected:
3
6640
by: Danny Din | last post by:
Hi, I have the following code – public class Class1 { public Class1()
3
5465
by: Wayne Brantley | last post by:
VS2005 RTM Create a web user control to use as a base class for other web user controls. Now, create a new web user control, change the class it inherits from to your base class and compile. (You must have a <% Register %> so it will see it) You will get TWO warnings per class like:
2
2140
by: Walt Zydhek | last post by:
Could someone tell me how to Change the parameter type for an override of an inherited class member? In particular, I would like to change an indexer for my class that implements the IList class. Such as this: public abstract class myclass : IList { private ArrayList ary; .... public string this
0
825
by: source | last post by:
I am tyring to get inhertied members of a type using relfection, but i am not able to do it. What bindingflags do I need to use to get inhertied members. Any pointers will help source
4
4779
by: Alan T | last post by:
I got a method in my ancestor form declared as Protected, this method has empty body. In my descendant form I declared as Protected also, then compile has no problem but the name of the method has green underline. The warning of the compilation is about the method hides inherited member. Use the new keyword if hiding was intened.
1
1134
by: Dinis Correia | last post by:
Hi all, How can I replace an inherited member name? I've built a class that inherits from KeyedCollection(Of ..., ...). I would like to replace base class Items property with Fields property. Is this possible? TIA, DC
5
1788
by: Anders Borum | last post by:
Hello I'm developing an API with a number of concrete collections that inherit from an abstract collection hierarchy. Each inheritance level provides an implementation of IEnumerable<T>, thus allowing the developer to take full advantage of the LINQ extensions without having to start with a cast operation to a concrete type that matches the...
0
7697
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7612
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7672
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7968
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5512
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1212
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
937
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.