How do I get a constructor to call another constructor

T

tshad

I want to set up a class that takes 2 arguments in its parameters list.

But I also want to allow a second constructor that takes one argument that
will call the 2nd Constructor and pass the variable and a blank (for the
parameter that wasn't passed.

I know this isn't correct but how would I change this to do what I want?

class temp
{
public temp(arg1)
{
temp(arg1,"");
}

public temp(arg1, arg2)
{
code here...
if (arg2 == "")
{
}
}
}

Thanks,

Tom
 
J

Josip Medved

But I also want to allow a second constructor that takes one argument that
will call the 2nd Constructor and pass the variable and a blank (for the
parameter that wasn't passed.

class temp
{
public temp(arg1)
: this(arg1,"")
{
}


public temp(arg1, arg2)
{
code here...
if (arg2 == "")
{
}
}
}
 
J

Jason Newell

You're wanting an overloaded constructor.

class temp
{
public temp(object arg1) : this(arg1, null)
{
}

public temp(object arg1, object arg2)
{
}
}

Jason Newell
Software Engineer
www.jasonnewell.net
 
M

miher

Hi,

Use the this keyword to call other constructor and the base keyword to call
base type constructor.
For example:
public temp(arg1) : this(arg1,"") //type of parameter is missing....
{
}

Hope You find this useful.
-Zsolt
 
T

tshad

That was it.

Thanks,

Tom
Josip Medved said:
class temp
{
public temp(arg1)
: this(arg1,"")
{
}


public temp(arg1, arg2)
{
code here...
if (arg2 == "")
{
}
}
}
 
G

Guest

You need to write it like this:

public class temp
{
public temp(string arg1, string arg2)
{
//code here
}
public temp(string arg1)
: this(arg1, "")
{
//code here
}

}

-Chris Treml
 

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

Similar Threads


Top