将 class 中的变量值放入我的表单变量中

Putting the value of a variable from class to the variable of my form

我有一个 class 个名字 intersection_regular_island,我在 form1 中创建了它的一个新实例。我想在 class 中重新声明某个变量,所以我尝试了这段代码

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

你还没有学过方法吗?您只需将第二个 Select Case 放入方法中:

Private Function GetPen(arrowWidth As Single) As Pen
    Dim penToUse As Pen

    Select Case lightFunctionNum
            Case 1
                penToUse = New Pen(Color.Green, arrowWidth)
                Console.WriteLine("Trigger")
            Case 2
                penToUse = New Pen(Color.Yellow, arrowWidth)
            Case 3
                penToUse = New Pen(Color.Red, arrowWidth)
            Case 4
                Dim blinkColorTrue As Color = Color.Green
                Dim blinkColorFalse As Color = Color.Transparent
                If blinkBoolean = False Then penToUse = New Pen(blinkColorFalse, arrowWidth) : blinkBoolean = True Else penToUse = New Pen(blinkColorTrue, 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, arrowWidth) : blinkBoolean = True Else penToUse = New Pen(blinkColorTrue, arrowWidth) : blinkBoolean = False
            Case Else
                penToUse = New Pen(Color.Transparent, arrowWidth)

    End Select

    Return penToUse
End Function

并为第一个 Select Case:

调用该方法
Dim penToUse = GetPen(a.arrowWidth)

Select Case arrowAndPosition
    Case arrowAndPositionChoices.A_Left
        a.leftA_pen = penToUse
    Case arrowAndPositionChoices.B_Left
        a.leftB_pen = penToUse
    Case arrowAndPositionChoices.C_Left
        a.leftC_pen = penToUse
    Case arrowAndPositionChoices.D_Left
        a.leftD_pen = penToUse
End Select

事实上,您甚至不必将 arrowWidth 传入,因为您可以只使用 return 一个 Color 方法,然后在外部创建 Pen :

Dim penToUse = New Pen(GetPenColor(), a.arrowWidth)

Select Case arrowAndPosition
    Case arrowAndPositionChoices.A_Left
        a.leftA_pen = penToUse
    Case arrowAndPositionChoices.B_Left
        a.leftB_pen = penToUse
    Case arrowAndPositionChoices.C_Left
        a.leftC_pen = penToUse
    Case arrowAndPositionChoices.D_Left
        a.leftD_pen = penToUse
End Select