title: OOP concept: is it possible to update the class of an instantiated object?
body: <p>I am trying to write a simple program that should allow a user to save and display sets of heterogeneous, but somehow related data. For clarity sake, I will use a representative example of vehicles. The program flow is like this:</p>
  
  <ol>
  <li>The program creates a <strong>Garage</strong> object, which is basically a class that can contain a list of vehicles objects</li>
  <li>Then the users creates <strong>Vehicles</strong> objects, these <strong>Vehicles</strong> each have a property, lets say <em>License Plate Nr</em>. Once created, the <strong>Vehicle</strong> object get added to a list within the <strong>Garage</strong> object</li>
  <li>--<strong><em>Later on</em></strong>--, the user can specify that a given <strong>Vehicle</strong> object is in fact a <strong>Car</strong> object or a <strong>Truck</strong> object (thus giving access to some specific attributes such as <em>Number of seats</em> for the Car, or <em>Cargo weight</em> for the truck)</li>
  </ol>
  
  <p>At first sight, this might look like an OOP textbook question involving a base class and inheritance, but the problem is more subtle because at the object creation time (and until the user decides to give more info), the computer doesn't know the exact Vehicle type.</p>
  
  <p>Hence my question: <strong>how would you proceed to implement this program flow? Is OOP the way to go?</strong></p>
  
  <p>Just to give an initial answer, here is what I've came up until now. There is only one Vehicle class and the various properties/values are handled by the main program (not the class) through a dictionary. However, I'm pretty sure that there must be a more elegant solution (I'm developing using VB.net):</p>
  
  <pre><code>Public Class Garage
      Public GarageAdress As String
      Private _ListGarageVehicles As New List(Of Vehicles)
  
      Public Sub AddVehicle(Vehicle As Vehicles)
          _ListGarageVehicles.Add(Vehicle)
      End Sub
  End Class
  
  Public Class Vehicles
      Public LicensePlateNumber As String
      Public Enum VehicleTypes
          Generic = 0
          Car = 1
          Truck = 2
      End Enum
      Public VehicleType As VehicleTypes
      Public DictVehicleProperties As New Dictionary(Of String, String)
  End Class
  </code></pre>
  
  <p>NOTE that in the example above the public/private modifiers do not necessarily reflect the original code</p>