Link to home
Start Free TrialLog in
Avatar of JoseHidalgo
JoseHidalgoFlag for Costa Rica

asked on

UserControl with Generics


Hi

I have my Color Picker Control which have ChildWinEditLayout Parent property. Now I want to use generics. I want to have my NewChildWinEditLayout Parent or OldChildWinEditLayout to be set as MyParent.

How can I make this code only have one constructor using generics ?

public partial class ColorPickerControl : UserControl
  {
   
    OldChildWinEditLayout myParent;
    NewChildWinEditLayout myParentControl;
    string tag;

    public ColorPickerControl(OldChildWinEditLayout myParent1, string tag1)
    {
      tag = tag1;
      myParent = myParent1;
      InitializeComponent();
    }

    public ColorPickerControl(NewChildWinEditLayout myParent1, string tag1)
    {
        tag = tag1;
        myParentControl = myParent1;
        InitializeComponent();
    }

}

Open in new window


Also my ColorPicker control has this line of code

myParent.SetForeColor("UpDown");

Open in new window


How can I enforce that classes that use my ColorPicker Color must have SetForeColor property.

Thanks
SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of JoseHidalgo

ASKER

I create an interface like this:

public interface IColorPicker
    {
        void SetForeColor(string s);
    }

Open in new window


then

public partial class NewChildWinEditLayout : IColorPicker
public partial class OldChildWinEditLayout : IColorPicker

Open in new window


finally

public void SetForeColor(string s)
    {
      txtForeColor.Text = s;
    }

Open in new window


When I compile the code it show me an error in ColorPickerControl that says:

"Error      1      'object' does not contain a definition for 'SetForeColor' and no extension method 'SetForeColor' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)"

    Object myParent;
    string tag;

    public ColorPickerControl(Object myParent1, string tag1)
    {
      tag = tag1;
      myParent = myParent1;
      InitializeComponent();
    }  

    if (tag == "b")
    {
       //myParent.SetBackColor(s);
    }
    else
    {
       myParent.SetForeColor(s);
    }

Open in new window


How can I solve this issue?
Fixed

    IColorPicker myParent;
    string tag;

    public ColorPickerControl(IColorPicker myParent1, string tag1)
    {
      tag = tag1;
      myParent = myParent1;
      InitializeComponent();
    }    

Open in new window

Thanks