user3493747
user3493747

Reputation: 13

How to declare a constructor?

I get the following error when I compile the program

"Microsoft.Samples.Kinect.ControlsBasics.SelectionDisplay' does not contain a constructor that takes 2 argument"

I probably need to declare another constructor for the new thing I've created, but I don't know how to do that. I've posted the respective codes below, could you help me please?

public SelectionDisplay(string itemId)
{
    this.InitializeComponent();

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

var files = Directory.GetFiles(@".\GalleryImages");

foreach (var file in files)
{
    FileInfo fileInfo = new FileInfo(file);

    BitmapImage bi = new BitmapImage();
    bi.BeginInit();
    bi.UriSource = new Uri(file, UriKind.Relative);
    bi.EndInit();

    var button = new KinectTileButton
    {
        Label = System.IO.Path.GetFileNameWithoutExtension(file),
        Background = new ImageBrush(bi),
        Tag = file
    };
    var selectionDisplay = new SelectionDisplay(button.Label as string, button.Tag as string);
    this.wrapPanel.Children.Add(button);
}

private void KinectTileButtonClick(object sender, RoutedEventArgs e)
{
    var button = (KinectTileButton)e.Source;
    var image = button.CommandParameter as BitmapImage;
    var selectionDisplay = new SelectionDisplay(button.Label, button.Background); // aici poti apoi sa mai trimiti si imaginea ca parametru pentru constructor
    this.kinectRegionGrid.Children.Add(selectionDisplay);
    e.Handled = true;
}

http://i61.tinypic.com/nno384.png

http://i57.tinypic.com/33vm2k7.png

Thanks in advance!

EDIT:now i have a different problem.. i get three new errors after i've made these changes. Take a look at the new image with the changes made

http://i58.tinypic.com/qwwqvn.png

Upvotes: 0

Views: 394

Answers (3)

Guilherme Oliveira
Guilherme Oliveira

Reputation: 2028

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

Upvotes: 2

Sandeep
Sandeep

Reputation: 1

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

Upvotes: 0

David
David

Reputation: 218798

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)
{
    //...
}

Upvotes: 3

Related Questions