TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Gary
NA
22
0
Newbie : Issues with constructor chaining
Jun 13 2012 5:59 PM
Hi All
I'm not sure if there is a forum for Newbies, so I'm going to post this here and if there is a FAQ on this and a newbie section then please let me know.
I hope you can help a newbie to C# programming with something that I am struggling regarding constructor chaining.
If I have multiple fields of the same type that I want to be populated on instaniation is there a way to indicate which field want to be populated in the chained call.
For example I have a class that has 2 private properties/variable:
class ClassName
{
// Field data.
private int id;
private int age;
// Chained constructors
ClassName() : this(0) {}
ClassName(int myID) : this(myID, 0) { }
ClassName(int myAge) : this(0, myAge) { }
// Master constructor
ClassName(int myID, int myAge)
{
id = myID;
age = myAge;
}
}
Main would look like this say:
public static void Main()
{
// Is this argumentfor ID or Age as they are both of type int?
ClassName myClass = new class(10);
}
I hope you can see what my problem is? Is there a way to allocate values to exact constructor arguments or is this an issue with constructor chaining that has to be lived with, and I should ignore chaining when dealing with constructors that contain arguments with this same type?
Many thanks
Gary
Reply
Answers (
11
)
Short key for ordering the code lines in C#.NET
Calculate disk space