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 / C# / June 2007

Tip: Looking for answers? Try searching our database.

Class inheritance constructor questino ...

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jamie Risk - 28 Jun 2007 22:02 GMT
Two classes:

public class _A {
  public int Value1;
  public int Value2;

  public _A(int v1, int v2) {
    this.Value1 = v1;
    this.Value2 = v2;
  }
}

public class _B : _A {
  public int Value3;

  public _B(_A a, v3) : base(a.Value1, a.Value2) {
     this.Value3 = v3;
  }
}

Is there a way I can create a constructor for _B without have to
break apart the elements of the _A typed argument when calling
the base constructor?
Jon Skeet [C# MVP] - 28 Jun 2007 22:11 GMT
> Two classes:
>
[quoted text clipped - 19 lines]
> break apart the elements of the _A typed argument when calling
> the base constructor?

I'm not sure what you mean - could you elaborate?

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Peter Duniho - 28 Jun 2007 23:16 GMT
>> Is there a way I can create a constructor for _B without have to
>> break apart the elements of the _A typed argument when calling
>> the base constructor?
>
> I'm not sure what you mean - could you elaborate?

I assume he means he'd like to do "public _B(_A a, v3) : base(a)..." or  
something similar even when _A doesn't define a constructor that takes an  
instance of an _A.

As far as I know, it's not possible.  But I've been wrong before.  :)

Pete
Jon Skeet [C# MVP] - 28 Jun 2007 23:30 GMT
> >> Is there a way I can create a constructor for _B without have to
> >> break apart the elements of the _A typed argument when calling
[quoted text clipped - 7 lines]
>
> As far as I know, it's not possible.  But I've been wrong before.  :)

Correct. On the other hand, it's perfectly possible for a constructor
to take an instance of the same type and copy field values over etc.

What you also can't do is try to create a new object which still uses
the old object for its base fields, permanently linking the two
together. Of course, you can keep a reference to the old object and
keep referring to it that way, using composition instead of (or as well
as) inheritance.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Jamie Risk - 29 Jun 2007 14:29 GMT
> What you also can't do is try to create a new object which still uses
> the old object for its base fields, permanently linking the two
> together.

This exactly what I was trying to ask.
Peter Duniho - 29 Jun 2007 17:28 GMT
>> What you also can't do is try to create a new object which still uses  
>> the old object for its base fields, permanently linking the two  
>> together.
>
> This exactly what I was trying to ask.

Really?  That wasn't clear _at all_, for what it's worth.  Your example  
appeared to be asking simply about copying the values from the old object,  
not tying the old object permanently to the new one.
Chris Dunaway - 28 Jun 2007 22:32 GMT
> Two classes:
>
[quoted text clipped - 21 lines]
> break apart the elements of the _A typed argument when calling
> the base constructor?

Add a constructor to your A class that takes an instance of A:

public class _A {
>    public int Value1;
>    public int Value2;
[quoted text clipped - 3 lines]
>      this.Value2 = v2;
>    }

    //second constructor
    public _A(_A a) {
    }
>}

Then you can change your _B constructor like this:

> public class _B : _A {
>    public int Value3;
[quoted text clipped - 4 lines]
>
> }

I hope I've understood your question correctly.

Chris

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.