Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / February 2005

Tip: Looking for answers? Try searching our database.

OOP???

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Dean L. Howen - 27 Feb 2005 16:04 GMT
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.
Carl Daniel [VC++ MVP] - 27 Feb 2005 17:36 GMT
> Ex: I have 2 classes like this:
>
[quoted text clipped - 25 lines]
>
> 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
[quoted text clipped - 11 lines]
>
> 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
[quoted text clipped - 10 lines]
> 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

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.