I have written the following script to adhere to the guidleines set out in the following question from college :
Write a class with three data fields. Then create three constructors, each with a different number of parameters. The constructors with fewer than three parameters should call another constructor, which contains the actual implementation to set all the fields' values.
It seems to work except for the fact that when I only enter 1 parameter the compiler complains that there is no overload for the method that takes 1 argument.
Whats confusing me is that I have read that this is the way to call a constructor which supplies all the paramters in "C# Unleashed" by Joesph Mayo. Should they be actually be enclosed with ""?
Expand|Select|Wrap|Line Numbers
- using System;
- public class Data
- {
- string userName;
- string idNumber;
- string nickName;
- // constructors
- public Data()
- : this ("No username", "No idNumber", "No nickName") {}
- public Data(string newUserName, string newIDNumber)
- : this (newUserName, newIDNumber, "No nickName") {}
- public Data(string newUserName,
- string newIDNumber,
- string newNickName)
- {
- userName = newUserName; // should this
- idNumber = newIDNumber; //implementation have
- nickName = newNickName; // this format "nickName =
- // "newNickName"?
- }
- static void Main()
- {
- Data myDataConstructor = new Data("blimey");
- Console.WriteLine("Name is : " + myDataConstructor.userName);
- Console.WriteLine("id number is : " + myDataConstructor.idNumber);
- Console.WriteLine("nickName is : " + myDataConstructor.nickName);
- }
- }