vb.net - Return an inherited type from a method -
suppose have next classes defined:
public class baseclass ... end class public class derivedclass inherits baseclass ... fields, methods, etc ... end class
and then, in code, have function signature of:
public function dosomething(...) list(of baseclass)
and when seek , homecoming object of type list(of derivedclass)
it, error:
value of type 'system.collections.generic.list(of baseclass)' cannot converted 'system.collections.generic.list(of derivedclass)'
i know not fields of derivedclass
filled, give me needed.
is there way this, or considered bad programming practice? and, if so, right way this?
have @ this:
http://msdn.microsoft.com/en-us/library/dd799517(v=vs.110).aspx
understanding covariance , contravariance clear things bit :)
covarianceenables utilize more specific type specified. can assign instance of ienumerable (ienumerable(of derived) in visual basic) variable of type ienumerable.
example:
ienumerable<derived> d = new list<derived>(); ienumerable<base> b = d;
contravariance enables utilize more generic (less derived) type specified. can assign instance of ienumerable (ienumerable(of base) in visual basic) variable of type ienumerable.
example:
action<base> b = (target) => { console.writeline(target.gettype().name); }; action<derived> d = b; d(new derived());
invariance means can utilize type specified; invariant generic type parameter neither covariant nor contravariant. cannot assign instance of ienumerable (ienumerable(of base) in visual basic) variable of type ienumerable or vice versa.
vb.net visual-studio
No comments:
Post a Comment