Overloading contructors // newbie questions

G

genc ymeri

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);
}
 
A

Andreas Håkansson

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
 
G

genc ymeri

I see.
Thanks a lot.

Andreas Håkansson said:
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
 
R

Ravichandran J.V.

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
 

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