对不起,如果标题不是很清楚。这是一个 VB.NET (2010) 问题
我有一个名为“Device”的超类,它有许多继承它的子类。其中一些子类也有子类。特别是,我有一个名为“TwinCatIntegerDevice”的类,它继承了“TwinCatDevice”,它继承了“Device”。
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,我是初学者)
至关重要的是,我将设备作为 Device 超类传递(这样我就不必为每个子类型设置单独的子例程)。
当我在将名为“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 设备类在被转换为其超类设备时显然具有不同的设置点变量?
如果这看起来有点不连贯,我很抱歉。任何帮助都会很棒!甚至关于形式或其他任何事情。我仍在尝试解决整个 VB.NET 问题。