Type Mismatch

  • Thread starter Thread starter Dave T
  • Start date Start date
D

Dave T

I have a type *Address* and a type *ResidentialAddress* which inherits
Address. When I try to execute SOMECLASS.ResAddr = OTHERCLASS.Addr I get the
inevitable type mis-match error "unable to cast type ObjectModel.Address to
Type ObjectModel.ResidentialAddress" I was thinking that since
ResidentialAddress inherits Address, there must be some way to do this
assignment
 
Dave said:
I have a type *Address* and a type *ResidentialAddress* which inherits
Address. When I try to execute SOMECLASS.ResAddr = OTHERCLASS.Addr I get the
inevitable type mis-match error "unable to cast type ObjectModel.Address to
Type ObjectModel.ResidentialAddress" I was thinking that since
ResidentialAddress inherits Address, there must be some way to do this
assignment

You're trying to assign an Address to a ResidentialAddress. Not every
Address is a ResidentialAddress. OTHERCLASS.Addr can also be a WhateverAddress
which is not a ResidentialAddress. Or it can just be an Address. Therefore it fails.

If you know that OTHERCLASS.Addr _always_ returns a ResidentialAddress,
you can type cast the reference
SOMECLASS.ResAddr = DirectCast(OTHERCLASS.Addr, ResidentialAddress)

Or, if possible, the type of OTHERCLASS.Addr can be changed to ResidentialAddress.
 
Hello Dave,

Yea but commonly your properties have the same names if you don't cast them
using an interface.

I would not use a class name Address, that is to ambiguous (to often used)
in Net.

Cor
 
Back
Top