Inherits, Base Class and CType

Hi!

Is this possible in VB.Net and if it is, does anyone have some sourcecode to share cause I can't seem to get it right :(

Public MustInherit Class MyBaseClass

End Class

Public Class clsA
Inherits MyBaseClass
En Class

Public Class clsB
Inherits MyBaseClass
En Class

Dim A as new clsA
Dim B as new clsB

A = CType(B, clsA)

Want i'm trying to do is to create a new A object that the base class values already set to B's base class values.

Any suggestions ?

Regards,
Per

[567 byte] By [PerBornsjo] at [2007-12-24]
# 1
You cannot cast a clsB instance to clsA, the two classes are not related. The best you can do is cast to the base class:
Dim base As MyBaseClass = A
nobugz at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 2

Thanks for your quick reply :)

If I cant cast them directly, is there another way to set the properties, of the base class in the clsA, based on the base class of clsB, other then going thorugh all properties and do clsA.Property = clsB.Property ?

Regards
Per

PerBornsjo at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 3
Note

that a cast won't copy the properties. I would make sure that

MyBaseClass has the properties that both derived classes have in

common. Then add a CopyFrom() method that looks something like

this:

Public MustInherit Class MyBaseClass

Private mField As Integer

Public Property Field() As Integer

Get

Return mField

End Get

Set(ByVal value As Integer)

mField = value

End Set

End Property

'... Other field and properties...

Public Sub CopyFrom(ByVal other As MyBaseClass)

Field = other.Field

'...etc

End Sub

End Class

nobugz at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 4

Okey, i'll do it this way then :)

Thanks for the help!

//Per

PerBornsjo at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic Language...