PC Review


Reply
Thread Tools Rate Thread

Constructor woes

 
 
Shikari Shambu
Guest
Posts: n/a
 
      2nd Aug 2004
Hi,
I have a class that is derived from another class.

Say, Class Child dervies from Class Parent

Class Parent has a constructor that takes two params

public Parent(string A, string B)

I want Class Child to have a constructor that takes just one param but calls
the parents two param constructor

Something like

public Child (string A)
{
B = "Hello";
// Call the two param constructor here
}

I had to do a new to acheive this

public Child(string A)
{
B = "Hello";
// Call the two param constructor here
new Child(A, B)
}

Is there any other ways of doing what I did? And, what is the impact of the
new within the constructor?

TIA


 
Reply With Quote
 
 
 
 
Mattias Sjögren
Guest
Posts: n/a
 
      2nd Aug 2004
Shikari,

>Is there any other ways of doing what I did?


You probably want:

public Child(string A) : base(A, "Hello")
{
}



Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
 
Reply With Quote
 
=?Utf-8?B?R2lyaXNoS3VtYXI=?=
Guest
Posts: n/a
 
      3rd Aug 2004
Hi Shambu,

What Mattias has mentioned is the right way of invoking a super class constructor. In your case, you are simply creating an instance of super class object(though the instance cannot be accessed becasue an identifier is not provided for the same).

When you invoke a super class constructor -as mentioned by Mattias- a super class instance is not created, but the features inherited by the subclass will get initialized
--
Girish Kumar
ICICI Infotech


"Mattias Sjögren" wrote:

> Shikari,
>
> >Is there any other ways of doing what I did?

>
> You probably want:
>
> public Child(string A) : base(A, "Hello")
> {
> }
>
>
>
> Mattias
>
> --
> Mattias Sjögren [MVP] mattias @ mvps.org
> http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
> Please reply only to the newsgroup.
>

 
Reply With Quote
 
=?Utf-8?B?R2lyaXNoS3VtYXI=?=
Guest
Posts: n/a
 
      3rd Aug 2004
Hi Shambu,

What Mattias is mentioned is the right way of invoking a base class constructor.
In your , you are simply creating an instance of base class(though this object cannot be accessed as an identifier is not provided).

When we invoke a base class constructor -in the way Mattias has mentioned, a base class object is not created, but the featurs inherited by the sub class will get initialized.

--
Girish Kumar
ICICI Infotech


"Mattias Sjögren" wrote:

> Shikari,
>
> >Is there any other ways of doing what I did?

>
> You probably want:
>
> public Child(string A) : base(A, "Hello")
> {
> }
>
>
>
> Mattias
>
> --
> Mattias Sjögren [MVP] mattias @ mvps.org
> http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
> Please reply only to the newsgroup.
>

 
Reply With Quote
 
=?Utf-8?B?R2lyaXNoS3VtYXI=?=
Guest
Posts: n/a
 
      3rd Aug 2004
oops!
I received an ERR on first submit, so made a second submit. Now both have been added..!
--
Girish Kumar
ICICI Infotech


"GirishKumar" wrote:

> Hi Shambu,
>
> What Mattias is mentioned is the right way of invoking a base class constructor.
> In your , you are simply creating an instance of base class(though this object cannot be accessed as an identifier is not provided).
>
> When we invoke a base class constructor -in the way Mattias has mentioned, a base class object is not created, but the featurs inherited by the sub class will get initialized.
>
> --
> Girish Kumar
> ICICI Infotech
>
>
> "Mattias Sjögren" wrote:
>
> > Shikari,
> >
> > >Is there any other ways of doing what I did?

> >
> > You probably want:
> >
> > public Child(string A) : base(A, "Hello")
> > {
> > }
> >
> >
> >
> > Mattias
> >
> > --
> > Mattias Sjögren [MVP] mattias @ mvps.org
> > http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
> > Please reply only to the newsgroup.
> >

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C# constructor best practice: how much logic to place in a C#constructor Jon Microsoft C# .NET 2 11th Nov 2009 09:18 PM
overloaded constructor, access zero-argument constructor mblatch Microsoft C# .NET 3 8th Apr 2005 09:53 PM
Calling a struct constructor in a class constructor body Karl M Microsoft VC .NET 4 19th Dec 2004 01:21 PM
How to know inside a constructor who called the constructor? Efkas Microsoft C# .NET 2 25th Nov 2004 05:23 PM
Calling overloaded constructor with values inside another constructor Tristan Microsoft C# .NET 2 30th Mar 2004 01:05 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:50 PM.