Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Overloading contructors // newbie questions

0 views
Skip to first unread message

genc ymeri

unread,
Mar 23, 2004, 5:22:29 PM3/23/04
to
Hi,
I have a form class which I would like to have two overloaded contructors.
But I want the second constructor to call the first too. How can I cal lthe
first constractor within the second one ?

Thanks in advance

public frmUI4Properties()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
MessageBox.Show("First Constructor");


//
// TODO: Add any constructor code after InitializeComponent call
//
}

public frmUI4Properties(string Hello)
{
//???How can I call the first constructor ?
MessageBox.Show(Hello + "Second contructor);
}

Andreas Håkansson

unread,
Mar 23, 2004, 5:32:02 PM3/23/04
to
Genc,

You should use the "this" keyword to instruct the second constructor to
call your default constructor. Please note that if you call your second
constructor, then the call will *first* be forwarded to your default
constructor and then the constructor you called.

public frmUI4Properties()
{


InitializeComponent();
MessageBox.Show("First Constructor");
}

public frmUI4Properties(string Hello) : this()
{
MessageBox.Show(Hello + "Second contructor);
}

If you have more consturctors and you want to call a specific overload
then you just provide the right parameters, in the correct order, to the
this() statement.

Hope this helps,

//Andreas

"genc ymeri" <genc_...@hotmail.com> skrev i meddelandet
news:ekxPVVSE...@tk2msftngp13.phx.gbl...

genc ymeri

unread,
Mar 23, 2004, 5:36:19 PM3/23/04
to
I see.
Thanks a lot.

"Andreas Håkansson" <andy.h (at) telia.com> wrote in message
news:ul5KraSE...@TK2MSFTNGP09.phx.gbl...

Ravichandran J.V.

unread,
Mar 24, 2004, 1:20:00 AM3/24/04
to
Yes, but please post the code and not the whole of the Web Form Designer
stuff. Then the code can be added on to it. It will be easier for you.

with regards,


J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandran+J.V.&cob=aspnetpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID=P3966388&BN=999&PN=2
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Jon Skeet [C# MVP]

unread,
Mar 24, 2004, 2:48:31 AM3/24/04
to
genc ymeri <genc_...@hotmail.com> wrote:
> I have a form class which I would like to have two overloaded contructors.
> But I want the second constructor to call the first too. How can I cal lthe
> first constractor within the second one ?

See http://www.pobox.com/~skeet/csharp/constructors.html

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

genc ymeri

unread,
Mar 24, 2004, 11:27:01 AM3/24/04
to
Thanks.


"Jon Skeet [C# MVP]" <sk...@pobox.com> wrote in message
news:MPG.1acb4daca...@msnews.microsoft.com...

genc ymeri

unread,
Mar 24, 2004, 11:26:55 AM3/24/04
to
>Yes, but please post the code and not the whole of the Web Form Designer
> stuff.

Huh ????

"Ravichandran J.V." <jvravic...@yahoo.com> wrote in message
news:eXCNKgW...@TK2MSFTNGP12.phx.gbl...

0 new messages