If you don't want to read all this, look at the code (notice the different return types) and read below it :p
Basically, I'm being very anal and don't want any compiler warnings at all :p I've been getting this annoying level 2 warning in VS.NET saying that my inheriting classes are hiding a property, and I need to use the new keyword. The thing is, I don't want to use the new keyword because I want the inherited method to always handle it, and the base method to only be there as a requirement for all inherited classes. Some of you may know what I'm getting at already, but if not, here's what's going on:
I have a RegistryValue class (my own) and several other classes such as StringValue, DwordValue, MultiStringValue, etc. The base class, RegistryValue, has an abstract property called Data that returns an object (because a registry value can contain numerous types of objects). The inheriting classes also have a Data property that currently hides this base property, and returns a value of the correct type (string for StringValue, int for DwordValue, string[] for MultiStringValue, etc). This works great, but VS.NET wants me to declare the Data property in the inherited members with the new keyword, giving any client apps access to both the base and inherited properties. But I don't want that :p I say cast it to an object yourself if that's what you need, but I can't fathom why you would... I would just remove the base property, but in the future someone may add additional types of RegistryValues, and I think those new types should be required to have this property. So how do I do this? :huh: Here's some sample code if anyone needs it:
public abstract class RegistryValue
{
public object Data
{
? get
? {
?? return this.data;
? }
? set
? {
?? this.data = value;
? }
}
}
public class DwordValue : RegistryValue
{
public int Data
{
? get
? {
?? return (int)this.data;
? }
? set
? {
?? base.data = (object)value;
? }
}
}[/quoteSo how do I get around this compiler warning while still requiring all inherited classes to have a Data property, but allow that inherited property to return any type it wants?y:huh: it wants? :huh:
Sorry for the long-winded post, I hope I didn't waste too much o:blush:e's time :blush:
Question
John Veteran
If you don't want to read all this, look at the code (notice the different return types) and read below it :p
Basically, I'm being very anal and don't want any compiler warnings at all :p I've been getting this annoying level 2 warning in VS.NET saying that my inheriting classes are hiding a property, and I need to use the new keyword. The thing is, I don't want to use the new keyword because I want the inherited method to always handle it, and the base method to only be there as a requirement for all inherited classes. Some of you may know what I'm getting at already, but if not, here's what's going on:
I have a RegistryValue class (my own) and several other classes such as StringValue, DwordValue, MultiStringValue, etc. The base class, RegistryValue, has an abstract property called Data that returns an object (because a registry value can contain numerous types of objects). The inheriting classes also have a Data property that currently hides this base property, and returns a value of the correct type (string for StringValue, int for DwordValue, string[] for MultiStringValue, etc). This works great, but VS.NET wants me to declare the Data property in the inherited members with the new keyword, giving any client apps access to both the base and inherited properties. But I don't want that :p I say cast it to an object yourself if that's what you need, but I can't fathom why you would... I would just remove the base property, but in the future someone may add additional types of RegistryValues, and I think those new types should be required to have this property. So how do I do this? :huh: Here's some sample code if anyone needs it:
Link to comment
Share on other sites
13 answers to this question
Recommended Posts