user2413453
user2413453

Reputation:

Issue with : Extension method must be static

I have this error.

First Error:

Extension method must be static

Second Error:

'System.Windows.Controls.ComboBox' does not contain a definition for 'GetClientItemId' and no extension method 'GetClientItemId' accepting a first argument of type 'System.Windows.Controls.ComboBox' could be found (are you missing a using directive or an assembly reference?)

I tried to apply static to my parent class like:

Before -

public partial class pgRPhase : Window

After -

public static partial class pgRPhase : Window

But I then receive other errors and issues.

WPF Coding -

private async Task LoadRep(TruckServiceClient TSC, ComboBox combobox)
{

        List<ClientItems> clientItems = new List<ClientItems>();            
        foreach (var rep in await TSC.GetRepByCompAsync())                
        clientItems.Add(new ClientItems { Id = rep.Id, Name = rep.Name, Contact = rep.ContactNumber, Email = rep.Email, CompanyId = rep.CompanyId });
        combobox.ItemsSource = (clientItems.ToArray().Where(x => x.CompanyId == cbCustomer.GetClientItemId()));
        combobox.IsEnabled = true;
        combobox.SelectedIndex = 1;

        combobox.DisplayMemberPath = "Id";

}

public static int GetClientItemId(this ComboBox combobox)
{
    if (combobox.SelectedItem == null)
        return 0;
    else
        return ((ClientItems)combobox.SelectedItem).Id;
}

Upvotes: 1

Views: 273

Answers (1)

Patrick Hofman
Patrick Hofman

Reputation: 156978

You have to put your GetClientItemId in a separate class and make that class static. (Extension methods require to be in a static class)

By making your Window class static, you break your entire code (you can't instantiate an instance of the window, which is required for WPF to work). If you split off the method, it will work.

Note: making this an extension method is nice, but it would probably work good enough when you make it just a regular method that is called through GetClientItemId(cbCustomer). Then you don't need another class.

Upvotes: 2

Related Questions