FilipF36
FilipF36

Reputation: 1

C# Method has 1 parameter but is invoked with 2 arguments

I have tried searching for information without any luck.

I've got a couple of Classes, for example CarManufacturerModel & CarModelModel.

In CarManufacturerModel I create some car makes, "Audi, mercedes, bmw", and in CarModelModel I create the Manufacturer's different models.

I want to create an object(s) of CarModelModel in CarManufacturerModel so that I can add the following models of that manufacturer in the object declaration. One manufacturer can for example have multiple models, 1-series, 2-series, etc.

Here's CarModelModel

    public class CarModelModel
{
    //public string CarModel { get; set; }
    public ObservableCollection<string> CarModels { get; set; }

    public CarModelModel(/*string carModel*/ObservableCollection<string>carModels)
    {
        //CarModel = carModel;
        CarModels = carModels;
    }

    public static CarModelModel Create(/*string carModel*/ObservableCollection<string>carModels)
        => new CarModelModel(/*carModel*/carModels);

}

Here's CarManufacturerModel - notice some code is commented out

 public class CarManufacturerModel
{
    public string CarManufacturer { get; set; }

    public CarModelModel CarModel { get; set; }

    //public ObservableCollection<CarModelModel> CarModels { get; set; }

    private CarManufacturerModel(string carManufacturer, /*ObservableCollection<CarModelModel>carModels*/CarModelModel carModel)
    {
        CarManufacturer = carManufacturer;
        //CarModels = carModels;
        CarModel = carModel;

    }

    public static CarManufacturerModel Create(string carManufacturer, /*ObservableCollection<CarModelModel>carModels*/CarModelModel carModel)
        => new CarManufacturerModel(carManufacturer, carModel);

}

Here's the class where i declare the objects, CarManufacturerViewModel

public class CarManufacturerViewModel : ViewModelBase, ICarManufacturerViewModel
{
    private string _carYear;

    //private CarModelModel _selectedCarModel;
    private CarManufacturerModel _carManufacturer;

    //private ObservableCollection<CarModelModel> _carModels;
    private ObservableCollection<CarManufacturerModel> _carManufacturers;

    [DesignOnly(true)]
    public CarManufacturerViewModel()
    {
        CarManufacturers = new ObservableCollection<CarManufacturerModel>
        {
            CarManufacturerModel.Create("Audi", CarModelModel.Create("1", "2")) <---------------------
        };
        //SelectedCarManufacturer = CarManufacturers.First();
    }

    public ObservableCollection<CarManufacturerModel> CarManufacturers
    {
        get => _carManufacturers;
        set
        {
            SetProperty(ref _carManufacturers, value);
        }
    }

    public CarManufacturerModel SelectedCarManufacturer
    {
        get => _carManufacturer;
        set
        {
            SetProperty(ref _carManufacturer, value);
            //LoadSelectedCar();
        }
    }

I get the error message "Method 'create' has 1 parameter(s) but is invoked with 2 argument(s)..

What am I doing wrong that would allow me to add multiple models to 1 manufacturer in the same object declaration?

Update

public CarManufacturerViewModel()
    {
        CarManufacturers = new ObservableCollection<CarManufacturerModel>
        {
            CarManufacturerModel.Create("Audi", CarModelModel.Create(new string[]{"A1","A2","A3","A4","A5"})),
            CarManufacturerModel.Create("Mercedes", CarModelModel.Create(new string[]{"A-Class", "B-Class", "C-Class", "E-Class", "S-Class"})),
            CarManufacturerModel.Create("BMW", CarModelModel.Create(new string[]{"1-Serie", "2-Serie", "3-Serie", "4-Serie", "5-Serie"})),
            CarManufacturerModel.Create("Volkswagen", CarModelModel.Create(new string[]{ "Golf", "Passat", "Arteon", "T-Cross","Up!"})),
            CarManufacturerModel.Create("Volvo", CarModelModel.Create(new string[]{"V60","V70","XC60","XC90","S90"}))
        };
        //SelectedCarManufacturer = CarManufacturers.First();
    }

I try to use this in my ListView now but it only shows string[]array

 <ListView Grid.Row="1"
                      Grid.Column="0"
                      Margin="0"
                      ItemsSource="{Binding CarManufacturers}"
                      SelectedItem="{Binding SelectedCarModel, Mode=TwoWay}">
                <ListView.ItemContainerStyle>
                    <Style BasedOn="{StaticResource ListViewItemBase}"
                           TargetType="{x:Type ListViewItem}"></Style>
                </ListView.ItemContainerStyle>
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <Grid Margin="0">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition/>
                                <ColumnDefinition/>
                            </Grid.ColumnDefinitions>
                            <TextBlock Style="{StaticResource ResourceKey=ComboBoxItemTextBlock}"
                                       Text="{Binding CarModel.CarModelList}"/>
                        </Grid>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>

CarModelModel looks like this now.

 public class CarModelModel
{

    public string[] CarModelList { get; set; }

    public CarModelModel(string[]carModelList)
    {
        CarModelList = carModelList;
    }

    public static CarModelModel Create(string[]carModelList)
        => new CarModelModel(carModelList);
}

Upvotes: 0

Views: 2991

Answers (1)

canton7
canton7

Reputation: 42225

Your CarCarModel.Create method:

public static CarModelModel Create(ObservableCollection<string> carModels)

takes an ObservableCollection<string> as its single parameter.

You're trying to call it like:

CarModelModel.Create("1", "2")

That is, you're trying to pass two string parameters. However, it takes an ObservableCollection<string>, so you need to pass it an ObservableCollection<string>:

CarModelModel.Create(new ObservableCollection<string>() { "1", "2" });

I would question whether you want to use an ObservableCollection<string> here at all: you normally only use those in a ViewModel, and only when you're binding them through to the UI, and you're going to be adding and removing elements (and what those additions and removals to be shown in some UI element).

If you use an array of strings here instead:

public static CarModelModel Create(string[] carModels)

then you can declare it as a params array:

public static CarModelModel Create(params string[] carModels)

This means that you can call it as:

CarModelModel.Create("1", "2");

and the compiler will turn this into a call to create a new array for you:

CarModelModel.Create(new string[] { "1", "2" });

Also, I don't think your Create methods add anything: just use a normal constructor.

Upvotes: 4

Related Questions