C# Constructors

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Madmartigan
    New Member
    • Dec 2006
    • 23

    C# Constructors

    Hi all

    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 ""?


    Code:
     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);
     	}
     }
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    You only have three constructors listed there.
    A constructor that takes no arguments.
    A constructor that takes two arguments.
    A constructor that takes three arguments.

    It is absolutly correct that the compiler complains that there is no constructor that takes only argument, because you don't HAVE a constructor that takes one argument.

    Comment

    • Madmartigan
      New Member
      • Dec 2006
      • 23

      #3
      Thanks Plater

      Does this mean I should always provide the same amount of constructors for the number of parameters passed?

      Forgive me if thats a dumb question, I'm still very new to C#.

      M

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        Well, if you want to call a constructor with one argument, then yes you should have a constructor with one argument.

        Its no different then a function call, the same number of arguments required need to be passed in.

        Comment

        Working...