Click here to Skip to main content
15,896,063 members
Home / Discussions / C#
   

C#

 
AnswerRe: Cast Action to Action Pin
Pete O'Hanlon30-Mar-11 22:34
mvePete O'Hanlon30-Mar-11 22:34 
GeneralRe: Cast Action to Action Pin
Adriaan Davel30-Mar-11 22:46
Adriaan Davel30-Mar-11 22:46 
GeneralRe: Cast Action to Action Pin
Pete O'Hanlon30-Mar-11 23:21
mvePete O'Hanlon30-Mar-11 23:21 
GeneralRe: Cast Action to Action Pin
Adriaan Davel30-Mar-11 23:52
Adriaan Davel30-Mar-11 23:52 
GeneralRe: Cast Action to Action Pin
Eddy Vluggen31-Mar-11 0:03
professionalEddy Vluggen31-Mar-11 0:03 
GeneralRe: Cast Action to Action Pin
RobCroll31-Mar-11 2:32
RobCroll31-Mar-11 2:32 
AnswerRe: Cast Action to Action Pin
J4amieC30-Mar-11 23:22
J4amieC30-Mar-11 23:22 
AnswerRe: Cast Action to Action Pin
BobJanova30-Mar-11 23:28
BobJanova30-Mar-11 23:28 
Generic inheritance is a bit complicated. Here's the reason that you can't cast 'down' the generic type parameter inheritance tree.

Imagine this test class:

class BadMistake {
  void CompleteHandler(Dictionary<string,int> complexParameter){
    // some stuff
  }
  
  GenericBase<Dictionary<string,int>> instance;

  public void CauseDeath(){
    instance = new GenericBase<Dictionary<string,int>>(CompleteHandler);
    ((Base)instance).OnComplete(16);
  }
}


Now, CompleteHandler is expecting to be passed a Dictionary, and you can't compile something which would cause that not to be the case. But using the base class, you can pass an int (or anything else) to it.

Method parameter compatibility for generics works backwards. This example will compile if the type parameter in the base is a subclass of T, for example try:
public class Base
{
   public Func<T> OnComplete {get;set;} where T:Panel
   public Base(Func<T> onComplete) where T:Panel
   {
      OnComplete = onComplete;
   }
}
public class GenericBase<T> : Base
{
   public GenericBase(Func<T> onComplete) where T:UserControl : base(onComplete)
   {
      OnComplete = onComplete;
   }
}

(if I got the syntax on the constructor there right).

Also, there isn't really a reason to make a non generic base type and inherit from it in this way. You can just use a GenericBase<object> if you want one that can deal with anything.
AnswerRe: Cast Action to Action Pin
Rob Philpott31-Mar-11 1:39
Rob Philpott31-Mar-11 1:39 
GeneralRe: Cast Action to Action Pin
PIEBALDconsult31-Mar-11 2:59
mvePIEBALDconsult31-Mar-11 2:59 
GeneralRe: Cast Action to Action Pin
Rob Philpott31-Mar-11 3:24
Rob Philpott31-Mar-11 3:24 
QuestionSolution directory Pin
Helfdane30-Mar-11 11:59
Helfdane30-Mar-11 11:59 
AnswerRe: Solution directory Pin
Luc Pattyn30-Mar-11 12:08
sitebuilderLuc Pattyn30-Mar-11 12:08 
GeneralRe: Solution directory Pin
Helfdane30-Mar-11 21:17
Helfdane30-Mar-11 21:17 
GeneralRe: Solution directory Pin
Luc Pattyn30-Mar-11 22:06
sitebuilderLuc Pattyn30-Mar-11 22:06 
GeneralRe: Solution directory Pin
Helfdane30-Mar-11 22:08
Helfdane30-Mar-11 22:08 
AnswerRe: Solution directory Pin
Eddy Vluggen30-Mar-11 12:24
professionalEddy Vluggen30-Mar-11 12:24 
AnswerRe: Solution directory Pin
RobCroll30-Mar-11 13:46
RobCroll30-Mar-11 13:46 
AnswerRe: Solution directory Pin
GenJerDan30-Mar-11 14:50
GenJerDan30-Mar-11 14:50 
GeneralRe: Solution directory Pin
Henry Minute30-Mar-11 14:54
Henry Minute30-Mar-11 14:54 
GeneralRe: Solution directory Pin
Helfdane30-Mar-11 21:10
Helfdane30-Mar-11 21:10 
QuestionDataBinding.CurrentItemChanged Pin
eddieangel30-Mar-11 10:22
eddieangel30-Mar-11 10:22 
AnswerRe: DataBinding.CurrentItemChanged Pin
SledgeHammer0130-Mar-11 11:01
SledgeHammer0130-Mar-11 11:01 
GeneralRe: DataBinding.CurrentItemChanged Pin
eddieangel31-Mar-11 6:17
eddieangel31-Mar-11 6:17 
AnswerRe: DataBinding.CurrentItemChanged Pin
Henry Minute30-Mar-11 14:58
Henry Minute30-Mar-11 14:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.