Extending a base class to a derived class

We have two classes BasicRace & AdvancedRace. AdvancedRace inherits from BasicRace

I have a BasicRace but i want to 'convert' it to the advanced class.

See code below as a example:

Module Module1
    Sub Main()
        Dim bRace As New BasicRace With {.CourseID = 1, .MeetingDate = "2013-05-01", .RaceNumber = 1}
        Dim aRace As New AdvancedRace

        ' Upgrade bRace to AdvancedRace???????
    End Sub
End Module

Public Class BasicRace
    Public Property MeetingDate As Date
    Public Property CourseID As Integer
    Public Property RaceNumber As Integer
End Class

Public Class AdvancedRace
    Inherits BasicRace
    Public Property RaceTitle As String
End Class

Any help would be great - I'm starting to think it can not be done unless i write a function to convert a basicRace to AdvancedRace going through each property one by one?


You can't "convert" from a base class to a subclass as such (you can't change the type of an existing object), but you can create new instance of the subclass that copies the properties from your base class.

Typical ways of implementing this for your classes might be:

  • a constructor in AdvancedRace that takes a BasicRace parameter and copies properties from it
  • a static method in AdvancedRace that takes a BasicRace parameter, creates the new object with copied properties, and then returns it

It's worth noting that this will result in two completely separate objects (one of each type) that aren't linked at all - changes in your AdvancedRace object won't be reflected in the BasicRace or vice-versa.

Need Your Help

Using WebGL 3D Shapes as 2D Canvas Images

html5 canvas html5-canvas webgl html5-animation

I'm creating a 2D Canvas game with cubes raining down on players. Their goal is to shoot or dodge them. Right now the cubes are just 2D JavaScript OOP entities. What I want to do is replace the 2D ...

Control a service on a remote server from IIS

asp.net authentication iis-6 invalidoperationexception servicecontroller

Please note: In each step I describe below I'm logged in as the same domain user account.

How Can I Display a N x N Matrix of Random Numbers in Java?

java arrays matrix for-loop

I'm trying to create a matrix of random double numbers. The matrix must be of size n x n and all numbers must be between 1 and 100. I've been trying to sort it out for ages now and I know it must be