|
-
Feb 1st, 2006, 12:55 PM
#1
[RESOLVED] overload constructors
in C#, how do you call a classes constructor from an overloaded constructor?
For example.
Code:
public class Test
{
private string _arg1 = string.Empty;
private string _arg2 = string.Empty;
public Test(string Arg1)
{
_arg1 = Arg1;
}
public Test(string Arg1, string Arg2)
{
Test(Arg1);
_arg2 = Arg2;
}
}
In VB.NET, since you use call the constructor sub "New" you can have many New subs as long as it obeys overloading rules. Then you can call the first constructor from within the second one.. to cut down on duplicate coding...
how would I do this in C#? I am sure its just a syntax thing that I am not familiar with.
-
Feb 1st, 2006, 01:01 PM
#2
Re: overload constructors
I figured it out by searching around. This would be the proper way (incase anyone else runs into this)
Code:
public class Test
{
private string _arg1 = string.Empty;
private string _arg2 = string.Empty;
public Test(string Arg1)
{
_arg1 = Arg1;
}
public Test(string Arg1, string Arg2):this(Arg1)
{
_arg2 = Arg2;
}
}
-
Feb 1st, 2006, 03:58 PM
#3
Re: [RESOLVED] overload constructors
Yes. Also if you wanted to call the constructor that was defined ina BASE class then you'd use
Code:
public Test(string Arg1, string Arg2):base(Arg1)
{
_arg2 = Arg2;
}
I don't live here any more.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|