locked
c# constructors issues RRS feed

  • Question

  • User410067686 posted

    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.

    Monday, July 28, 2014 9:59 AM

Answers

  • User281315223 posted

    public Class(string brand, string model)
    {
         _brand = brand;
         _model = model;
    }
    
    For example , I just need model, how do I create a null for this

    You would just need to use :

    Class x = new Class(null,"Your Model");

    or :

    Class x = new Class("","Your Model");
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 28, 2014 10:49 AM

All replies

  • User-1360095595 posted

    These method/constructors are identical. They take a string as a parameter. That's the cause if the error. 

    Why not create just one constructor that takes two string parameters. 

    Monday, July 28, 2014 10:04 AM
  • User410067686 posted

    But I just need a single parameter

    Monday, July 28, 2014 10:09 AM
  • User-1360095595 posted

    Pass empty string for the other. 

    Once again, you can't have two constructors that take the same number/type of parameter. 

    Monday, July 28, 2014 10:17 AM
  • User410067686 posted

    what u mean for this, can give illustration? Thanks :)

    Monday, July 28, 2014 10:20 AM
  • User281315223 posted

    The compiler doesn't really work that way, it sees that both constructors for your particular class accept a single string (it doesn't care what the string looks like) so it assumes that the signatures of each method are identical and thus ambiguous.

    You have a few options as detailed below.

    Create an Additional Constructor

    If you create an additional constructor, you could always have the option of simply passing in either a null or empty string for the parameter you aren't concerned about at the time :

    private string _brand = "";
    private string _model = "";
    
    public Class(string brand)
    {
         _brand = brand;
    }
    
    public Class(string brand, string model)
    {
         _brand = brand;
         _model = model;
    }

    This way if you only had one value to populate it with (either brand or model), you could just pass in one or the other :

    Class x = new Class("Your Brand", null);

    Using an Empty Constructor

    If you create an empty constructor, it would allow you to use the anonymous syntax as seen below to set each of your individual properties. You would likely need to make a publicly accessible interface to use this approach however :

    private string _brand = "";
    private string _model = "";
    
    public string Brand { get { return _brand; } set { _brand = value; } }
    public string Model { get { return _model; } set { _model = value; } }
    
    public Class()
    {
    
    }

    which you could use through :

    Class x = new Class(){ Brand = "Your Brand", Model = null };

     

    Monday, July 28, 2014 10:24 AM
  • User410067686 posted
    public Class(string brand, string model)
    {
         _brand = brand;
         _model = model;
    }

    For example , I just need model, how do I create a null for this
    Monday, July 28, 2014 10:25 AM
  • User281315223 posted

    public Class(string brand, string model)
    {
         _brand = brand;
         _model = model;
    }
    
    For example , I just need model, how do I create a null for this

    You would just need to use :

    Class x = new Class(null,"Your Model");

    or :

    Class x = new Class("","Your Model");
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 28, 2014 10:49 AM
  • User410067686 posted
    public Class(null, string model)
    {
         null = null
         _model = model;
    }

    Can I do in this way?
    Monday, July 28, 2014 10:56 AM