locked
C# override RRS feed

  • Question

  • Hi guys,

    I'm having some troubles with a little code I consider dumb. But I'm stuck!

    When I try to compile this code, VS will tell me that DalStub.User.Entity should be Entities.AEntity so it can invalidate Dal.ADal.Entity.

    public abstract class AEntity
    {
      /* ... */
    }
    
    public abstract class ADal
    {
      public virtual AEntity Entity { get; set; }
      /* ... */
    }
    
    public class User : AEntity
    {
      public string Name { get; set; }
    }
    
    public class DalStub : ADal
    {
      public override User Entity { get; set; }
    }
    
    Wednesday, March 30, 2011 3:55 PM

Answers

  • This is typically better handled via generics:

    ]

    public abstract class ADal<T> where T: AEntity
    {
     public abstract T Entity { get; set; }
    }
    
    public class User : AEntity
    {
     public string Name { get; set; }
    }
    
    public class DalStub : ADal<User>
    {
     public override User Entity { get; set; }
    }
    
    


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Proposed as answer by Jesse HouwingMVP Thursday, March 31, 2011 9:35 PM
    • Marked as answer by Aspen VJ Wednesday, April 6, 2011 9:43 AM
    Wednesday, March 30, 2011 4:42 PM
  • When overriding something, you are not allowed to change the type of it. Here you want to override the property Entity, which is AEntity. But then you want it as User, which is different.

    You CAN use "new" instead of "override", but be careful with it:

    public class DalStub : ADal
    {
     public new User Entity { 
      get { return base.Entity as User;} 
      set{ base.Entity=value;} }
    }
    
    
    • Proposed as answer by Aspen VJ Friday, April 1, 2011 2:30 AM
    • Marked as answer by Aspen VJ Wednesday, April 6, 2011 9:43 AM
    Wednesday, March 30, 2011 4:06 PM

All replies

  • When overriding something, you are not allowed to change the type of it. Here you want to override the property Entity, which is AEntity. But then you want it as User, which is different.

    You CAN use "new" instead of "override", but be careful with it:

    public class DalStub : ADal
    {
     public new User Entity { 
      get { return base.Entity as User;} 
      set{ base.Entity=value;} }
    }
    
    
    • Proposed as answer by Aspen VJ Friday, April 1, 2011 2:30 AM
    • Marked as answer by Aspen VJ Wednesday, April 6, 2011 9:43 AM
    Wednesday, March 30, 2011 4:06 PM
  • This is typically better handled via generics:

    ]

    public abstract class ADal<T> where T: AEntity
    {
     public abstract T Entity { get; set; }
    }
    
    public class User : AEntity
    {
     public string Name { get; set; }
    }
    
    public class DalStub : ADal<User>
    {
     public override User Entity { get; set; }
    }
    
    


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Proposed as answer by Jesse HouwingMVP Thursday, March 31, 2011 9:35 PM
    • Marked as answer by Aspen VJ Wednesday, April 6, 2011 9:43 AM
    Wednesday, March 30, 2011 4:42 PM
  • Hi Carlos,

     

    First, I think we should know the limitation of the override method.

     

    1.  You cannot override a non-virtual or static method. The overridden base method must be virtual, abstract, or override.

    2.  An override declaration cannot change the accessibility of the virtual method. Both the override method and the virtual method must have the same access level modifier.

    3.  You cannot use the following modifiers to modify an override method:
    new static virtual abstract

    4.  An overriding property declaration must specify the exact same access modifier, type, and name as the inherited property, and the overridden property must be virtual, abstract, or override.

    The fourth point just as VreatVolk said. Also, here is link about C# override mechanism.
    http://msdn.microsoft.com/en-us/library/ebca9ah3(VS.71).aspx

    But if you want to meet your request, just try the code Reed provided. Thanks.


    Vin Jin [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, April 1, 2011 2:37 AM
  • 3.  You cannot use the following modifiers to modify an override method:
    new static virtual abstract

    You can override with an abstract method.
    Sunday, April 3, 2011 11:50 PM