views:

39

answers:

1

Sorry if the title isn't very clear. This is a VB.NET (2010) question

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."

The relevant parts of Device look like this:

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

The TwinCatDevice class does not overload Setpoints or SetpointsTb. The TwinCatIntegerDevice class does:

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

Now, the problem. I try to set the setpoints using a subroutine like so:

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

(I know that is terrible code :X, I'm a beginner)

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).

When I do this for a TwinCatIntegerDevice called "ThisDevice" after it has been passed to the subroutine:

MsgBox("As Device: " & CType(ThisDevice, Device).Setpoints.Count.ToString & vbNewLine & _
                   "As TwinCatDevice: " & CType(ThisDevice, TwinCatDevice).Setpoints.Count.ToString & vbNewLine & _
                   "As TwinCatIntegerDevice: " & CType(ThisDevice, TwinCatIntegerDevice).Setpoints.Count.ToString)

I get the following (9 is the correct number of setpoints in this case):

As Device: 9
As TwinCatDevice: 9
As TwinCatIntegerDevice: 0

Does anyone know why the TwinCatInteger device class apparently has a different variable for Setpoints when it is cast as its superclass Device?

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.

A: 

I think what you want to do, is remove your "OverLoads" keyword on the "TwinCatIntegerDevice" class, and replace it with the keyword "Shadows".

Ben
Yes, I had that before. The problem with that, I think, is that when I have my subclass object cast as its superclass, it doesn't assign values to the shadowed properties. Is that not correct?
BASnappl