Subclass / Superclass - 如果将子类强制转换为其超类,是否有办法使用子类的重载属性?

时间:2010-09-13 22:09:24

标签: vb.net inheritance casting overloading subclassing

很抱歉,如果标题不是很清楚。这是一个VB.NET(2010)问题

我有一个名为“Device”的超类,它有许多继承它的子类。其中一些子类也有子类。特别是,我有一个名为“TwinCatIntegerDevice”的类,它继承了继承“Device”的“TwinCatDevice”。

Device的相关部分如下所示:

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

TwinCatDevice类不会使Setpoints或SetpointsTb过载。 TwinCatIntegerDevice类执行:

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

现在,问题。我尝试使用如下子程序设置设定值:

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

(我知道这是可怕的代码:X,我是初学者)

至关重要的是,我将设备作为设备超类传递(因此我不必为每个子类型设置单独的子例程。)

当我将一个名为“ThisDevice”的TwinCatIntegerDevice传递给子程序后执行此操作时:

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)

我得到以下内容(在这种情况下,9是正确的设定点数):

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

有人知道为什么TwinCatInteger设备类在将其转换为超类设备时显然具有不同的Setpoints变量吗?

如果这看起来有点不连贯,我很抱歉。任何帮助都会很棒!甚至关于形式或其他任何事情。我还在试图弄清楚这个整个VB.NET。

1 个答案:

答案 0 :(得分:0)

我认为您要做的是删除“TwinCatIntegerDevice”类中的“OverLoads”关键字,并将其替换为关键字“Shadows”。