Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 301
  • Last Modified:

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
0
JoseHidalgo
Asked:
JoseHidalgo
  • 3
2 Solutions
 
Bob LearnedCommented:
It sounds like you need to implement an interface, that defines the contract that the classes will have the SetForeColor property.
0
 
Easwaran ParamasivamCommented:
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
 
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

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now