Reputation: 549
I have a base class called KeyRatioSymbol. I have a class inheriting from KeyRatioSymbol called GuruFocusKeyRatioSymbol.
KeyRatioSymbol has this update method:
/// <summary>
/// Updates an existing key ratio symbol by setting the values from the new <see cref="KeyRatioSymbol"/>.
/// </summary>
public virtual void Update<T>(T newKeyRatioSymbol) where T : KeyRatioSymbol
{
this.Identifier = newKeyRatioSymbol.Identifier;
}
GuruFocusKeyRatioSymbol defines member CurrencyId and wants to override the update method to include the new CurrencyId when updating:
private int _CurrencyId;
/// <summary>
/// Gets or sets the CurrencyId
/// </summary>
public int CurrencyId { get { return this._CurrencyId; } set { this.SetProperty(ref this._CurrencyId, value); } }
public override void Update<GuruFocusKeyRatioSymbol>(GuruFocusKeyRatioSymbol newKeyRatioSymbol)
{
base.Update(newKeyRatioSymbol);
this.CurrencyId = x.CurrencyId; // Compiler error
}
Now the compiler complains: 'GuruFocusKeyRatioSymbol' does not contain a definition for 'CurrencyId'...
Why does the compiler not find the CurrencyId memeber in the class GuruFocusKeyRatioSymbol?
Regards!
Upvotes: 0
Views: 86
Reputation: 203820
In your Update
method you simply named the generic argument GuruFocusKeyRatioSymbol
, whereas you used the name T
in the parent. Naming the generic argument GuruFocusKeyRatioSymbol
doesn't mean it's going to be of your GuruFocusKeyRatioSymbol
type, you just now have two different types with that same name. If you name the generic method argument T
, which is the common convention, it will have the same behavior, but it will become apparent why objects of that type don't necessarily have a CurrencyID
; they could be any type of key ratio symbol, not just that specific one.
Upvotes: 2