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
JoseHidalgoAsked:
Who is Participating?
 
Easwaran ParamasivamConnect With a Mentor Commented:
Create generics on parent class of both OldChildWinEditLayout  and NewChildWinEditLayout. That can be used as a parameter to your constructor. Please refer http://www.15seconds.com/issue/031024.htm.

To enforce SetForeColor as mandatory. Define an interface that has property of SetForeColor and derive your Class from that Interface. In that case that would become mandatory.


0
 
Bob LearnedConnect With a Mentor Commented:
It sounds like you need to implement an interface, that defines the contract that the classes will have the SetForeColor property.
0
 
JoseHidalgoAuthor Commented:
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?
0
 
JoseHidalgoAuthor Commented:
Fixed

    IColorPicker myParent;
    string tag;

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

Open in new window

0
 
JoseHidalgoAuthor Commented:
Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.