How to hide a property ?....

  • Thread starter Thread starter Laurent
  • Start date Start date
L

Laurent

Hi all,

I have a base class, let's say

class A
{
...
public object Data
{
get { ....}
set { ... }
}
}

And a class B that inherits from A:

class B : A
{
B()
{
Data = new object[3];
}

public MyData
{
{ get { return (MyData) Data[0]; }
{ set { Data[0] = value; }
}

...
}

I would like to have the property Data in the class B "protected" because
this property is used internaly by B and cannot be changed.
I have several derived classes from A and each of them has their own
properties to expose.


Thanks
Laurent
 
Laurent,
I would like to have the property Data in the class B "protected" because
this property is used internaly by B and cannot be changed.
I have several derived classes from A and each of them has their own
properties to expose.

AFAIK, in C# (or VB.NET for that matter), it is not possible to change the
scope of a class member like it is in C++. It seems to me like you might
consider declaring "Data" to be protected in your base class (A) to begin
with. Why do you declare it as public only to turn around and try to make
it protected?

Another alternative would be using a "shadow property" by using the "new"
keyword on the "Data" property. This would allow you to declare a new
property with the same name, but using whatever scope you like and/or making
it read-only.
 
There's no nice way to do this since what you're trying to do break the
principle of inheritance. However what I have done in the past is override
the Data setter/getter method and ignore the content of those method.

For example

class B : A
{

public object Data
{
set {
// ignore this
}
get {
return null;
}
}
}


HTH,
 
Given your code, I'd like to ask why not just leave out the "set" part and
make it "read-only" by default?

Making properties "read-only" without explicitly said so will tends to
create difficult to debug problems if you forget it later...

Victor Hadianto said:
There's no nice way to do this since what you're trying to do break the
principle of inheritance. However what I have done in the past is override
the Data setter/getter method and ignore the content of those method.

For example

class B : A
{

public object Data
{
set {
// ignore this
}
get {
return null;
}
}
}


HTH,

--
Victor Hadianto
Blog: http://www.hadianto.net/destination

Laurent said:
Hi all,

I have a base class, let's say

class A
{
...
public object Data
{
get { ....}
set { ... }
}
}

And a class B that inherits from A:

class B : A
{
B()
{
Data = new object[3];
}

public MyData
{
{ get { return (MyData) Data[0]; }
{ set { Data[0] = value; }
}

...
}

I would like to have the property Data in the class B "protected" because
this property is used internaly by B and cannot be changed.
I have several derived classes from A and each of them has their own
properties to expose.


Thanks
Laurent
 
Basically, I'll recommand to make the class B "private", then create public
setClassBValue() and getClassBValue() for public access.

That'll fulfill your wishes, and give you greater flexibility for working
with the object.
 
Thanks for answer.

To answer the question: why I set "Data" as public, is because other derived
class used Data to store an "object" and do not need any conversion.
I know what I have to do: using Data in the base class is not very well
structured so I will implement the properties I need in each derived
classes.


Laurent
 
Back
Top