【发布时间】:2023-03-11 15:52:01
【问题描述】:
我有一个类名intersection_regular_island,我在form1 中创建了它的一个新实例。我想在该类中重新声明某个变量,所以我尝试了这段代码
Public Class form1
Dim a As New intersection_regular_island
Dim penToUse As Pen = a.leftA_pen
Sub sampleSub()
penToUse = New Pen(Color.Green, a.arrowWidth)
End Sub
End Class
如果我这样做,它会起作用
Public Class form1
Dim a As New intersection_regular_island
Sub sampleSub()
a.leftA_pen = New Pen(Color.Green, a.arrowWidth)
End Sub
End Class
请注意,这只是代码的一小部分,并不是实际代码。这是为了说明目的,所以我可能会错过一些东西。
好像我错过了一些如此简单的东西?
编辑:
我想这样做,这样我就可以轻松地执行select case
所以不要多次复制粘贴这段代码
Select Case lightFunctionNum
Case 1
a.leftA_pen = New Pen(Color.Green, a.arrowWidth)
Case 2
a.leftA_pen = New Pen(Color.Yellow, a.arrowWidth)
Case 3
a.leftA_pen = New Pen(Color.Red, a.arrowWidth)
Case 4
Dim blinkColorTrue As Color = Color.Green
Dim blinkColorFalse As Color = Color.Transparent
If blinkBoolean_A = False Then a.leftA_pen = New Pen(blinkColorFalse, a.arrowWidth) : blinkBoolean_A = True Else a.leftA_pen = New Pen(blinkColorTrue, a.arrowWidth) : blinkBoolean_A = False
Case 5
Dim blinkColorTrue As Color = Color.Yellow
Dim blinkColorFalse As Color = Color.Transparent
If blinkBoolean_A = False Then a.leftA_pen = New Pen(blinkColorFalse, a.arrowWidth) : blinkBoolean_A = True Else a.leftA_pen = New Pen(blinkColorTrue, a.arrowWidth) : blinkBoolean_A = False
Case Else
a.leftA_pen = New Pen(Color.Transparent, a.arrowWidth)
End Select
我希望它变成这样
Select Case arrowAndPosition
Case arrowAndPositionChoices.A_Left
penToUse = a.leftA_pen
Case arrowAndPositionChoices.B_Left
penToUse = a.leftB_pen
Case arrowAndPositionChoices.C_Left
penToUse = a.leftC_pen
Case arrowAndPositionChoices.D_Left
penToUse = a.leftD_pen
End Select
Select Case lightFunctionNum
Case 1
penToUse = New Pen(Color.Green, a.arrowWidth)
Console.WriteLine("Trigger")
Case 2
penToUse = New Pen(Color.Yellow, a.arrowWidth)
Case 3
penToUse = New Pen(Color.Red, a.arrowWidth)
Case 4
Dim blinkColorTrue As Color = Color.Green
Dim blinkColorFalse As Color = Color.Transparent
If blinkBoolean = False Then penToUse = New Pen(blinkColorFalse, a.arrowWidth) : blinkBoolean = True Else penToUse = New Pen(blinkColorTrue, a.arrowWidth) : blinkBoolean = False
Case 5
Dim blinkColorTrue As Color = Color.Yellow
Dim blinkColorFalse As Color = Color.Transparent
If blinkBoolean = False Then penToUse = New Pen(blinkColorFalse, a.arrowWidth) : blinkBoolean = True Else penToUse = New Pen(blinkColorTrue, a.arrowWidth) : blinkBoolean = False
Case Else
penToUse = New Pen(Color.Transparent, a.arrowWidth)
End Select
【问题讨论】:
-
您似乎认为
Dim penToUse As Pen = a.leftA_pen以某种方式在该变量和属性之间创建了一种关系,因此分配给变量也会分配给属性。这不是它的工作原理。如果您希望将某些内容分配给该属性,那么您必须将其分配给该属性。第二个代码 sn-p 有效,因为它有意义,而第一个代码无效,因为它没有意义。 -
@jmcilhinney 谢谢,是的,我就是这么想的。我仍然不明白你的意思,你能给我一个例子或者一个关于如何正确地做到这一点的链接吗?非常感谢。
-
我的意思是没有办法做你显然想做的事情。您可以使用支持指针的语言来执行此操作,方法是使用两个指向同一位置的指针,但 VB 不支持指针,所以您不能。为什么你首先要这样做?毫无疑问,有一种方法可以满足您的要求;只是不是你想的那样。
-
@jmcilhinney 谢谢先生,我已经编辑了我的问题,所以我可以详细解释它。非常感谢。