Click here to Skip to main content
15,888,286 members
Please Sign up or sign in to vote.
1.20/5 (2 votes)
See more:
C#
private string _brand = "";

private string _model = "";

Public Class (string brand)

{

     _brand = brand;
}

Public Class (string model)

{

     _model = model
}


There is an error stating the call is ambiguous, I need two constructors but only one parameter that is different.
Posted
Updated 28-Jul-14 4:16am
v2
Comments
Sergey Alexandrovich Kryukov 28-Jul-14 12:39pm    
This code won't even compile. Why asking questions if you don't show the same code as the code you have your problem with?
—SA
Matt T Heffron 28-Jul-14 12:54pm    
The error OP stated (call is ambiguous) IS a compile-time error.
Sergey Alexandrovich Kryukov 28-Jul-14 13:01pm    
Yes, of course. I should have said — the code would not compile not only because of this problem, but because of wrong capitalization of "Public".
—SA

The purpose of constructors is to create an instance of a class with a defined state.
Constructing an instance that has only part of the members initialized is breaking the above mentioned claim.

Languages like C++ or C# (and others) provide the concept of overloading of function names, which means that the same function name can denote different function implementations. The only way to distinguish the function (with the same name!) is with differeing number of parameters and/or differing parameter types on at least one parameter position. This list of parameter types is called the signature of a function. If the signature of two functions is different, overloading may be possible, otherwise, overloading is ambiguous.

Constructors are special functions (they have no return type and their name is the same as the name of the class they belong to, plus they have special means to initialize members (in C++) and select the needed base class constructors (if the class derives from a base class).

As pointed out in the other solutions, you violate the overloading concept (two functions (constructors in this case) have the same signature.

You class should also have a better name and your code is carelessly posted - this won't compile at all since capitalization is broken and since you did not post the whole class code.

Please make it easy to us to post an answer by post in the first place compilable code. The prize of your careless post is that it is severly voted down - the risk now is high that the whole question and associated discussions get completely deleted soon.

Please update your question by using the [Improve Question] link and place the fixed C# code into <pre lang="cs">...</pre> block (as was already done for you by some nice soul).

Your class should look something like:
C#
// Car class that encapsulates brand and model
class Car
{
   private string _brand;
   private string _model;
   public Car(string brand, string model) { _brand = brand; _model = model; }
   ...
}

Cheers
Andi
 
Share this answer
 
v2
The signature of both constructors is the same, as both of them take only one argument of type string.
To be able to differentiate between the two constructors you'd need to use two different types in each constructor definition.

Cheers!
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 28-Jul-14 12:40pm    
Correct, but didn't you notice that the code would not compile not only because of this problem, but because of wrong capitalization of "Public".? Voted 4 this time... :-)
—SA
If you look at the "signature" of those constructors they both take a single string. How is the compiler, or the caller for that matter, able to tell the difference between a brand and a model?

Why not have another parameter in a single constructor that informs both the compiler and the caller which string you are trying to update?

Or, determine which string it is from the content - again a single constructor

Or, don't pass the string in the constructor but have 2 methods such as UpdateBrand and UpdateModel

You might find this article useful An Intro to Constructors in C#[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 28-Jul-14 12:40pm    
Correct, but didn't you notice that the code would not compile not only because of this problem, but because of wrong capitalization of "Public".? Voted 4 this time... :-)
—SA
Richard Deeming 28-Jul-14 13:42pm    
Not to mention the incorrect capitalization of "Class". :)
Sergey Alexandrovich Kryukov 28-Jul-14 15:18pm    
No! "Class" here is not the keyword "class". This is the name of the type. Bad name, for sure, but it would compile.
—SA
Richard Deeming 28-Jul-14 15:28pm    
So it would. One of the few down-sides of a case-sensitive language.
CHill60 29-Jul-14 11:11am    
I hadn't noticed that at all! :hangs head: I'm slipping in my old age ... or perhaps I need to visit an optician :-)
If you want to initialize both model and brand, use one Constroctor for both.

VB
Public Class (string brand, string model)

{

     _brand = brand;
     _model = model;

}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900