我有以下代碼(PointLine
和CalculatedLine
既繼承Line
):如何在不將對象重新賦值給VB.NET中的另一個變量的情況下投射對象?
For Each line As Lines.Line In lines
''Since both Point and Calculated lines are stored in the same list in the XML files,
''we need to force them back to their original type before using them
Select Case line.GetType()
Case GetType(Lines.PointLine)
line = DirectCast(line, Lines.PointLine)
line.init()''ERROR:'init' is not a member of 'PerformanceValidation.EngineValidation.Limits.Components.Lines.Line'
myLimitSeries.Add(line.series)
Case GetType(Lines.CalculatedLine)
line = DirectCast(line, Lines.CalculatedLine)
line.init(dataTable)''ERROR:'init' is not a member of 'PerformanceValidation.EngineValidation.Limits.Components.Lines.Line'
myLimitSeries.Add(line.series)
End Select
Next
編輯抱怨line
不是類型PointLine
/CalculatedLine
,當我打電話line.init()
。
將原始對象轉換爲PointLine或CalculatedLine是很重要的,因爲我「初始化」它們以包含稍後將用到的計算信息。所以基本上,我不想創建一個類型爲PointLine或CalculatedLine的新變量來充當Castted Line的容器。
我試着創建一個適當類型的新行(正如我說的我不想在上面)並刪除原始行並將新行添加到列表中,但當然它抱怨了基礎列表被修改。
有沒有辦法將Line
暫時作爲PointLine
或CalculatedLine
而不影響列表?
謝謝!
是的,它做到了。非常乾淨,非常優雅。謝謝! –