Hiding Base Class Properites from derived class instance

  • Thread starter Thread starter Microsoft
  • Start date Start date
M

Microsoft

Base class:


class AssetBase
{
string _clli;

public string CLLI
{
get
{
return _clli;
}
set
{
_clli = value
}
}


}


In Derived Class I want to give different name to the base class property
and internally refer the base class property like below


class Asset: BaseAsset
{
public string Location
{
get
{
base.CLLI;
}
set
{
base.CLLI = value;
}
}
}



Now , when the developer use this dll in their project and create Asset
instance I want to hide the AssetBase Property CLLI , all i want to see them
is Location. I can do this by making base class properties to protected ,
but I want some of the property to be exposed to the developer
as it's defined in base class. How can I do that ?


Thanks
baski
 
Hi Baski,

You can move Asset and AssetBase into a separate assembly and make CLLI
internal. Remember to make the classes public so calling code can see them.

public class AssetBase
{
string _clli;

internal string CLLI
{
get
{
return _clli;
}
set
{
_clli = value;
}
}
}

public class Asset: AssetBase
{
public string Location
{
get
{
return base.CLLI;
}
set
{
base.CLLI = value;
}
}
}

Joe
 
Hi Joe,

It's actually in different dll's. The problem is I don't know which property
in the base class will be renamed in the derived class.

If the base class properties is not renamed in the derived class , I want
the developer to see as it's defined in base class from derived class
instance variable.

Thanks

Baski
 
Microsoft said:
In Derived Class I want to give different name to
the base class property

You can't (and shouldn't) do that, because then your derived-class
objects could not be used in situations where the base-class objects
were used; and that's essentially the purpose of inheritance.

Why do you want to do this renaming? Perhaps you could choose a more
generic name in the base class that would be suitable for both.

P.
 
We have base framework , which will be used for multiple clients , each
client will call the properties in different names. Base class will not be
used by the developer
any where. We are using the inheritance to get most
functionality(reusability) from base class and easily adopt the new client
with simple customization.

thanks
Baski
 
The C# solution to hiding is that a derived class member will hide a base
class member only if they have the same name and signature. If I understand
you correctly, you want some automatic mechanism to hide a base class
property if a derived class property accesses that base class property,
which isn't an available feature. The solution I provided still doesn't
give access to CLLI from an AssetBase reference outside the assembly, so
that is probably not what you need either.

Joe
 
The C# solution to hiding is that a derived class member will hide a base
class member only if they have the same name and signature.

I think that's a bit misleading - the overload resolution rules will
actually use methods in the derived class in preference to methods in the
base. Even if there is an exact match in the base, a derived version that
requires parameter type conversion will still be used.

Here are two other rules that are relevant here:

A field, property, event, or nested type hides all inherited members with
the same name (including all the overloaded inherited methods).

A method hides all non-method inherited members with the same name.
 
Back
Top