Home Vb.net Using child instance in a parent-typed variable
Reply: 0

Vb.net Using child instance in a parent-typed variable

user870 Published in March 24, 2018, 12:46 am

I'm more familiar with how Java does polymorphism so I might be doing it wrong in vb

I'm trying use polymorphism and inheritance to have one instance that can take on multiple forms, the issue is the variable is instantiated as the parent type and initialized as a child class instance but I can't access the child properties.

below is an example what i'm trying to do:

sub main()
  Dim animal as Animal           'instantiated as parent type
  'some code later
  animal= New Dog()              'no problems here
  dim legs = animal.legs         'this works
  dim fur = animal.fur           'this is an error 
end sub

Public Class Animal
  Property legs as integer
End Class 

Public Class Dog
  inherits Animal
  Property fur as Boolean = True
End Class

It would be great if i could get this to work.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.346488 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO