Avatar of JoseHidalgo
JoseHidalgo
Flag 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
C#.NET Programming

Avatar of undefined
Last Comment
JoseHidalgo

8/22/2022 - Mon
SOLUTION
Bob Learned

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
Easwaran Paramasivam

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
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?
JoseHidalgo

ASKER
Fixed

    IColorPicker myParent;
    string tag;

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

Open in new window

JoseHidalgo

ASKER
Thanks
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck