locked
Problem with interface generic parameter of a generic method RRS feed

  • Question

  •  

    Hi,

    I have a interface like this:

    public interface IDataBindableUserControl
    {
      object DataObject { set; }
    }
    

    Which I want use it in a generic method like this:

    FATabStripItem CreatePage<UCType>() 
      where UCType : UserControl, IDataBindableUserControl, new()
    {
      FATabStripItem result = new FATabStripItem();
    
      result.Tag = this;
    
      UserControl uc=new UCType();
      uc.Dock=DockStyle.Fill;
      uc.DataObject = Data; ////////// Compiler error here.
    
      result.Controls.Add(uc);
    
      return result;
    }
    

    the problem is that when compiling, I got the error message below on the line I prompted.

    Error    1    'System.Windows.Forms.UserControl' does not contain a definition for 'DataObject' and no extension method 'DataObject' accepting a first argument of type 'System.Windows.Forms.UserControl' could be found (are you missing a using directive or an assembly reference?)    F:\Users\Mohammad\Documents\Visual Studio 2008\Projects\MarineDesktop\MarineDesktop\Handlers\PageHandler.cs    29    16    MarineDesktop

     

    Can anybody tell me why this occurs?

     

    Thanx.

     

    Thursday, April 29, 2010 7:54 AM

Answers

  • Hi,

    By where UCType : UserControl, IDataBindableUserControl, new()
    You told to the compiler that the UCType inherits from UserControl and IDataBindableUserControl. it doesn't mean that UserControl inherits from IDataBindableUserControl too. so, you should modify your code as below.

    FATabStripItem CreatePage<UCType>() 
     where UCType : UserControl, IDataBindableUserControl, new()
    {
     FATabStripItem result = new FATabStripItem();
    
     result.Tag = this;
    
     UCType uc=new UCType();
     uc.Dock=DockStyle.Fill;
     uc.DataObject = Data; //no compiler error here.
    
     result.Controls.Add(uc);
    
     return result;
    }
    

    Sincerely,

    Yasser.


    Don't be stickler and wine with William Shakespeare after the solution :^)
    "And this our life, exempt from public haunt, finds tongues in trees, books in the running brooks, sermons in stones, and good in everything." William Shakespeare
    • Marked as answer by Mohammad Elmi Thursday, April 29, 2010 8:34 AM
    Thursday, April 29, 2010 8:27 AM

All replies

  • the DataObject you defined in the IDataBindableUserControl,you must implement it before you use it in the other class

    Thursday, April 29, 2010 8:11 AM
  • the DataObject you defined in the IDataBindableUserControl,you must implement it before you use it in the other class


    Disagree.

    IDataBindableUserControl is just an interface and should not have any implementation.

    Of course I implement it in type I pass to the method, but it is not source of the problem.

    Thursday, April 29, 2010 8:24 AM
  • Hi,

    By where UCType : UserControl, IDataBindableUserControl, new()
    You told to the compiler that the UCType inherits from UserControl and IDataBindableUserControl. it doesn't mean that UserControl inherits from IDataBindableUserControl too. so, you should modify your code as below.

    FATabStripItem CreatePage<UCType>() 
     where UCType : UserControl, IDataBindableUserControl, new()
    {
     FATabStripItem result = new FATabStripItem();
    
     result.Tag = this;
    
     UCType uc=new UCType();
     uc.Dock=DockStyle.Fill;
     uc.DataObject = Data; //no compiler error here.
    
     result.Controls.Add(uc);
    
     return result;
    }
    

    Sincerely,

    Yasser.


    Don't be stickler and wine with William Shakespeare after the solution :^)
    "And this our life, exempt from public haunt, finds tongues in trees, books in the running brooks, sermons in stones, and good in everything." William Shakespeare
    • Marked as answer by Mohammad Elmi Thursday, April 29, 2010 8:34 AM
    Thursday, April 29, 2010 8:27 AM
  • Thank you Yasser.
    Thursday, April 29, 2010 8:41 AM