title: Subclass/Superclass - If a subclass is cast as its superclass, is there a way to use the overloaded properties of the subclass?
body: <p>Sorry if the title isn't very clear.  This is a VB.NET (2010) question</p>
  
  <p>I have a superclass called "Device" which has a number of subclasses that inherit it.  Some of those subclasses also have subclasses.  In particular, I have a class called "TwinCatIntegerDevice" which inherits "TwinCatDevice" which inherits "Device."</p>
  
  <p>The relevant parts of Device look like this:</p>
  
  <pre><code>Public Class Device
  
  Private _Setpoints As New List(Of Double)
  Public Overridable Property Setpoints As List(Of Double)
      Get
          Return _Setpoints
      End Get
      Set(ByVal value As List(Of Double))
          _Setpoints = value
          _SetpointsTb.Clear()
          For Each setpoint In value
              Dim setpointTb As New TextBox
              setpointTb.Text = setpoint.ToString
              _SetpointsTb.Add(setpointTb)
          Next
      End Set
  End Property
  Private _SetpointsTb As New List(Of TextBox)
      Public Overridable Property SetpointsTb As List(Of TextBox)
          Get
              Return _SetpointsTb
          End Get
          Set(ByVal value As List(Of TextBox))
              _SetpointsTb = value
              Me._Setpoints.Clear()
              For Each setpoint In value
                  Me._Setpoints.Add(setpoint.Text)
              Next
          End Set
      End Property
  
  End Class
  </code></pre>
  
  <p>The TwinCatDevice class does not overload Setpoints or SetpointsTb.
  The TwinCatIntegerDevice class does:</p>
  
  <pre><code>Public Class TwinCatIntegerDevice
      Inherits TwinCatDevice
  
      Private _Setpoints As New List(Of Integer)
      Public Overloads Property Setpoints As List(Of Integer)
          Get
              Return _Setpoints
          End Get
          Set(ByVal value As List(Of Integer))
              _Setpoints = value
              _SetpointsTb.Clear()
              For Each setpoint In value
                  Dim setpointTb As New TextBox
                  setpointTb.Text = setpoint.ToString
                  _SetpointsTb.Add(setpointTb)
              Next
          End Set
      End Property
      Private _SetpointsTb As New List(Of TextBox)
      Public Overloads Property SetpointsTb As List(Of TextBox)
          Get
              Return _SetpointsTb
          End Get
          Set(ByVal value As List(Of TextBox))
              _SetpointsTb = value
              Me._Setpoints.Clear()
              For Each setpoint In value
                  Me._Setpoints.Add(setpoint.Text)
              Next
          End Set
      End Property
  
  End Class
  </code></pre>
  
  <p>Now, the problem.  I try to set the setpoints using a subroutine like so:</p>
  
  <pre><code>Private Sub FetchDeviceRecipe(ByRef device As Device, ByRef excelSheet As ExcelWorksheet, ByVal row As Integer)
          Dim lastCol As Integer = NumberOfProcessSteps + 1
          Try
              For col = 2 To lastCol
                  Dim setpoint As New TextBox
                  setpoint.Text = excelSheet.Cells(row, col).Value
                  device.SetpointsTb.Add(setpoint)
              Next
              device.SetpointsTb = device.SetpointsTb
          Catch ex As Exception
              MsgBox(ex.Message)
          End Try
      End Sub
  </code></pre>
  
  <p>(I know that is terrible code :X, I'm a beginner)</p>
  
  <p>Crucially, I'm passing the device as the Device superclass (so that I don't have to have a separate subroutine for each sub-type).</p>
  
  <p>When I do this for a TwinCatIntegerDevice called "ThisDevice" after it has been passed to the subroutine:</p>
  
  <pre><code>MsgBox("As Device: " &amp; CType(ThisDevice, Device).Setpoints.Count.ToString &amp; vbNewLine &amp; _
                     "As TwinCatDevice: " &amp; CType(ThisDevice, TwinCatDevice).Setpoints.Count.ToString &amp; vbNewLine &amp; _
                     "As TwinCatIntegerDevice: " &amp; CType(ThisDevice, TwinCatIntegerDevice).Setpoints.Count.ToString)
  </code></pre>
  
  <p>I get the following (9 is the correct number of setpoints in this case):</p>
  
  <pre><code>As Device: 9
  As TwinCatDevice: 9
  As TwinCatIntegerDevice: 0
  </code></pre>
  
  <p>Does anyone know why the TwinCatInteger device class apparently has a different variable for Setpoints when it is cast as its superclass Device?</p>
  
  <p>I'm sorry if this seems a bit incoherent.  Any help would be great!  Even regarding form or anything else.  I'm still trying to figure this whole VB.NET thing out.</p>