简体   繁体   English

如何在不将其重新分配给VB.NET中的另一个变量的情况下强制转换对象?

[英]How do I cast an object without re-assigning it to another variable in VB.NET?

I have the following code ( PointLine and CalculatedLine both inherit Line ): 我有以下代码( PointLineCalculatedLine都继承Line ):

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

The editor complains about line not being of type PointLine / CalculatedLine when i call line.init() . 当我调用line.init()时,编辑器抱怨line不是PointLine / CalculatedLine类型。

It's important that the original object be casted to either PointLine or CalculatedLine because I "initialize" them to contain calculated information that will be used later on. 将原始对象强制转换为PointLine或CalculatedLine非常重要,因为我将它们“初始化”为包含将在以后使用的计算信息。 So basically, I don't want to create a new variable of type PointLine or CalculatedLine to act as a container for the casted Line. 因此,基本上,我不想创建类型为PointLine或CalculatedLine的新变量来充当强制转换Line的容器。

I tried creating a new line of the appropriate type (exactly as I said I didn't want to above) and deleting the original line and adding the new one to the list but of course, it complains about the underlying list being modified. 我尝试创建适当类型的新行(正是我之前所说的那样),然后删除原始行并将新行添加到列表中,但是当然,它抱怨底层列表已被修改。

Is there a way I can treat a Line as either a PointLine or CalculatedLine temporarily without affecting the list? 有没有一种方法可以在不影响列表的情况下暂时将Line视为PointLineCalculatedLine

Thanks! 谢谢!

You cannot re-type an item in a collection, because the type is set for the whole collection (in your example it's end of line 1). 您不能在集合中重新输入项目的类型,因为该类型是为整个集合设置的(在您的示例中,它是在第1行的结尾)。

I'd take a decision, whether the source data are in correct/required format or not. 我将决定源数据是否为正确/必需的格式。 If you're not happy with the source format, I'd fix it there. 如果您对源格式不满意,请在此处进行修复。 If you need the source as is, I'd treat the lines in a layer above, but it would certainly require another collections. 如果您需要原样的源代码,我会在上面的一层中处理这些行,但是肯定需要其他集合。 Either you could convert one type of lines to another and put them into one collection or you could have one collection with some IDs and type, and two other collections for each type (you can probably use the source collections then). 您既可以将一种类型的行转换为另一种类型,然后将它们放入一个集合中,也可以拥有一个带有某些ID和类型的集合,并为每种类型创建另外两个集合(然后可以使用源集合)。

You don't need to create a new instance but you need to have a second variable of the right type for the reference. 您无需创建新实例,但需要为引用提供第二种类型正确的变量。 I don't understand when you say "delete", when you cast, you don't delete the instance since both variable are pointing at the same instance. 我不明白当您说“删除”时,在投射时,您不会删除该实例,因为两个变量都指向同一实例。

For Each line As Lines.Line In lines

    Select Case line.GetType()

    Case GetType(Lines.PointLine)
        Dim linePoint As Lines.PointLine = DirectCast(line, Lines.PointLine)

        linePoint.init()
        myLimitSeries.Add(line.series)

    Case GetType(Lines.CalculatedLine)
        Dim lineCalculated As Lines.CalculatedLine = DirectCast(line, Lines.CalculatedLine)

        lineCalculated.init(dataTable)
        myLimitSeries.Add(line.series)
    End Select
Next

No new instance are created here. 此处未创建新实例。

If you only call one function, you could always do. 如果仅调用一个函数,则可以始终执行。

For Each line As Lines.Line In lines

    Select Case line.GetType()

    Case GetType(Lines.PointLine)
        DirectCast(line, Lines.PointLine).init()
        myLimitSeries.Add(line.series)

    Case GetType(Lines.CalculatedLine)
        DirectCast(line, Lines.CalculatedLine).init(dataTable)
        myLimitSeries.Add(line.series)
    End Select
Next

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM