473,395 Members | 2,796 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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


Expand|Select|Wrap|Line Numbers
  1.  using System;
  2.  public class Data
  3.  {
  4.      string userName;
  5.      string idNumber;
  6.      string nickName;
  7.  
  8.      // constructors
  9.      public Data()
  10.          : this ("No username", "No idNumber", "No nickName") {}
  11.  
  12.      public Data(string newUserName, string newIDNumber)
  13.          : this (newUserName, newIDNumber, "No nickName") {}
  14.  
  15.      public Data(string newUserName,
  16.                  string newIDNumber,
  17.                  string newNickName)
  18.      {
  19.         userName = newUserName;     // should this
  20.         idNumber = newIDNumber;      //implementation have
  21.         nickName = newNickName;    // this format "nickName = 
  22.                                                                             // "newNickName"?
  23.      }
  24.      static void Main()
  25.      {
  26.          Data myDataConstructor = new Data("blimey");
  27.          Console.WriteLine("Name is : " + myDataConstructor.userName);
  28.          Console.WriteLine("id number is : " + myDataConstructor.idNumber);
  29.          Console.WriteLine("nickName is : " + myDataConstructor.nickName);
  30.      }
  31.  }
Mar 7 '08 #1
3 930
Plater
7,872 Expert 4TB
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.
Mar 7 '08 #2
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
Mar 7 '08 #3
Plater
7,872 Expert 4TB
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.
Mar 7 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Rajesh Garg | last post by:
Can we have private constructors and destructors? IF yes what is the use of such constructors or destructors.....in the sense where can these be implemented in a system................. I have...
42
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
6
by: Stephen Martinelli | last post by:
thanks for the help...just one more question.... can a class have more then two parameterized constructors?..i would like to be able to instanciate the class with a different number of...
4
by: Sathyaish | last post by:
What is a private constructor, and why would a class have one? What are the other kinds of constructors besides: (1) public constructors; and (2) parameterized constructors And I understand...
10
by: John | last post by:
Trying to find out what is essential / optional, I made an extremely simple Class and Module combination to add two numbers. (see below) It appears that an empty constructor is needed n order to...
3
by: John | last post by:
Before anything else, thanks Marina, Workgroups and Ralf, for your help so far. I am now able to better define the question! After adding more console printout lines to CSum, I tried all...
22
by: Peter Morris [Droopy eyes software] | last post by:
Look at these two classes public class Test { public readonly string Name; public Test(string name)
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.