How to declare a constructor?

后端 未结 3 708
逝去的感伤
逝去的感伤 2021-01-28 07:42

I get the following error when I compile the program

\"Microsoft.Samples.Kinect.ControlsBasics.SelectionDisplay\' does not contain a constructor that ta

相关标签:
3条回答
  • 2021-01-28 08:23

    You should create an overload of SelectionDisplay's contructor or change the one you already have. Like this:

    public SelectionDisplay(string itemId, string tag)
    {
        //Do something here
    }
    

    Due to you're creating a new instance of SelectionDisplay with two arguments, but its constructor only accept one argument. (string itemId):

    //foreach
    new SelectionDisplay(button.Label as string, button.Tag as string); //
    
    //KinectTileButtonClick method
    new SelectionDisplay(button.Label, button.Background);
    

    You have to check what type button.Label, button.Tag and button.Background are and create a new constructor with these values.

    You can read more about Constructors here

    0 讨论(0)
  • You have error in two lines:

    var selectionDisplay = new SelectionDisplay(button.Label as string, button.Tag as string);
    

    and

    var selectionDisplay = new SelectionDisplay(button.Label, button.Background); 
    

    and you define the constructor as

    public SelectionDisplay(string itemId)
        {
            this.InitializeComponent();
    
            this.messageTextBlock.Text = string.Format(CultureInfo.CurrentCulture,Properties.Resources.SelectedMessage,itemId);
    
        }
    

    if you need to define with some default value then you need to do like this

    public SelectionDisplay(string itemId, string nextParam="default value")
        {
            this.InitializeComponent();
    
            this.messageTextBlock.Text = string.Format(CultureInfo.CurrentCulture,Properties.Resources.SelectedMessage,itemId);
    
        }
    

    In this case you can either pass next argument or ignore it

    0 讨论(0)
  • 2021-01-28 08:36

    Well, you've already created a constructor that takes one argument:

    public SelectionDisplay(string itemId)
    {
        //...
    }
    

    But you're passing it two arguments:

    new SelectionDisplay(button.Label as string, button.Tag as string);
    

    You can add an argument to the constructor you have, or create a new one:

    public SelectionDisplay(string itemId, string someOtherValue)
    {
        //...
    }
    
    0 讨论(0)
提交回复
热议问题