vb.net - How to override properties in a derived class using generics -


i have following class setup , don't understand why won't compile.

i following error on "public overrides property f t"

  • 'public overrides property f t' cannot override 'public overridable property f x' because differ in return types.
  • this confuses me because constrained t derived x.

can explain how can accomplish goal? in end need have class , class b b inherits a. has overridable property f of type x, , b overrides f type derived x. suggestions appreciated. if cannot done, i'd interested know why (limitation of .net?) , how should go this.

public class x end class  public class y     inherits x end class  public class     public overridable property f x end class  public class a(of t x)     inherits     public overrides property f t end class  public class b     inherits a(of y)     public overrides property f y end class 

thank-you!

new answer. don't think it's possible (exactly requested it), namely b overrides f type derived x, saw.

but hold y in private field in b , expose through f. you'd need cast f y access whatever functionality y provides on x. can done without changing a.

public class x     public overridable function z() string         return "x"     end function end class  public class y     inherits x     public overrides function z() string         return "y"     end function     public function foo() string         return "bar"     end function end class  public class     public overridable property f x end class  public class b     inherits     private _f y     public overrides property f x                     return _f         end         set(value x)             _f = directcast(value, x)         end set     end property end class 

usage:

dim new a() dim x new x() dim b new b() dim y new y()  a.f = x console.writeline(a.f.z) ' console.writeline(directcast(a.f, y).foo()) ' invalidcastexception  b.f = y console.writeline(b.f.z) console.writeline(directcast(b.f, y).foo()) ' ok 

output

x
y
bar


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -