0

Hopefully this is a quick question. I have a class in C# which inherits from Window (WPF). I would like to treat this like a modal dialog, and am trying to make the dialog accept a generic type. The code is below:

public partial class PickingOptionsDialog<T> : Window
{
    private T _SelectedOption;
    public PickingOptionsDialog(ECartState State, String Bin)
    {
        InitializeComponent();
        DataContext = new OptionsViewModel(State, Bin);
    }

    public PickingOptionsDialog()
    {
        InitializeComponent();
        DataContext = new OptionsViewModel(ECartState.Picking, "1");
    }

    public T SelectedOption
    {
        get { return _SelectedOption; }
    }
}

With this code, I get a compile time error stating that InitializeComponent() doesnt exist in the current context. Why does it do this?

In addition to this problem, I would like to make it so that the generic type allowed has to be an Enum. In java (if Enum was a type) I could do this like < T extends Enum >. How can I accomplish this in C#?

Mark W
  • 2,791
  • 1
  • 21
  • 44
  • Please only ask one question per question. Also can you please try calling `base.InitializeComponent();`? – dav_i Dec 10 '14 at 15:06
  • 2
    T cannot be an Enum, only a class or a struct – dada686 Dec 10 '14 at 15:07
  • Possible duplicate of http://stackoverflow.com/questions/6925584/the-name-initializecomponent-does-not-exist-in-the-current-context – Ben Black Dec 10 '14 at 15:10
  • @dav_i calling base doesnt resolve. Same compile time error. dada686 Really? You cant use a generic Enum? This post makes it seem like thats not the case http://stackoverflow.com/questions/79126/create-generic-method-constraining-t-to-an-enum – Mark W Dec 10 '14 at 15:10
  • 1
    You cannot unfortunately check if **T** is an `enum` per se, you can however check if **T** is a struct e.g. `public class PickingOptionsDialog : Window where T : struct`, however you could possible wrap the enum into a some class and create an interface around it. – jacqijvv Dec 10 '14 at 15:15
  • Without any more details, I'm fairly certain that you somehow messed the XAML part, i.e. that it doesn't properly refer to you generic code behind. Could you post it as well? – decPL Dec 10 '14 at 15:15

1 Answers1

1

To answer your first question: You would have to add the type parameter to all parts of your class. In the WPF case, this means you would have to add it to the .xaml (because from that the implementation of InitializeComponent() is generated). Unfortunatelly, you cannot declare a template parameter in the xaml (at least I wouldn't know how). So the simple answer seems to be: This is not possible using WPF.

For the second question: You can use a "where" clause, as in

public class C<T> where T : struct
{
}

However, you can only limit T to be a value type (or some interface), but not explicitly an enum.

PMF
  • 14,535
  • 3
  • 23
  • 49
  • I just realized that the designer was generating a partial class that wasnt generic, and figured I would run into a WPF issue :( I suppose its possible with some effort, but I could just declare the return val Enum, remove the generics and call it a day. – Mark W Dec 10 '14 at 15:20