OOP???

D

Dean L. Howen

Ex: I have 2 classes like this:

class Parent
{
private string msg;
public string Message
{
get
{
return this.msg;
}
set
{
this.msg = value;
}
.......
}
};

class Child : Parent
{
Child ()
{
// do something with property "Message"
}
...
}


As the class I describe above, I have some questions. Please help me:
Q1: I just want the property "Message" is used directly when create instance
of class Parent

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Parent obj = new Parent();
obj.Message = "Parent"; // it's ok
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

but, I don't want it (the property "Message") be used if create instance of
class Child, although it could be used inside class Child
Child obj = new Child();
obj.Message = "Parent"; // it's not allowed

could we do something like those.

Q2: could we override properties in inheritance class. I want override
System.Data.DataTable.Rows in System.Data.DataTable class
ex:

class MyDataRowCollection : System.Data.DataRowCollection
{
.....
}

class MyDataTable : System.Data.DataTable
{
/// I want covert the property: base.Rows from System.Data.DataRowCollection
to MyDataRowCollection. Could we do this?
};

Please give me your ideas.
Thanks so much.
 
C

Carl Daniel [VC++ MVP]

Dean said:
Ex: I have 2 classes like this:

class Parent
{
private string msg;
public string Message
{
get
{
return this.msg;
}
set
{
this.msg = value;
}
.......
}
};

class Child : Parent
{
Child ()
{
// do something with property "Message"
}
...
}


As the class I describe above, I have some questions.

First, this is C# code, but this is a C++ newsgroup. You want
microsoft.public.dotnet.languages.csharp. I've set follow-ups to that group
and copied this message there as well.
Please help me:
Q1: I just want the property "Message" is used directly when create
instance of class Parent

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Parent obj = new Parent();
obj.Message = "Parent"; // it's ok
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

but, I don't want it (the property "Message") be used if create
instance of class Child, although it could be used inside class Child
Child obj = new Child();
obj.Message = "Parent"; // it's not allowed

could we do something like those.

Your design is flawed from an OO perspective, which is why you're having a
hard time making it do what you want. When you use derivation, you're
establishing an IsA (a Child IsA Parent) relationship. This implies that
anything the base class can do, the child class can do as well.

If you want the property to be visible in Parent but not visible in Child,
you need to use a form of composition other than derivation. Specifically,
you should embed an instance of Parent inside each instance of Child. You
may have to write forwarding functions to get Parent-like behavior from
Child in the places that you do what it. You might want to create an
interface that represents the common functionality that you do want (the
functionality that does satisfy the IsA relationship) and have both Parent
and Child implement that interface.
Q2: could we override properties in inheritance class. I want override
System.Data.DataTable.Rows in System.Data.DataTable class
ex:

class MyDataRowCollection : System.Data.DataRowCollection
{
....
}

class MyDataTable : System.Data.DataTable
{
/// I want covert the property: base.Rows from
System.Data.DataRowCollection to MyDataRowCollection. Could we do this?
};

C# does not support covariant property types. You can overload the property
by using the 'new' keyword, but it won't be an overload of a virtual
property in the base class. Again, containment instead of derivation is
probably what you want.

-cd
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top