【问题标题】:Putting the value of a variable from class to the variable of my form将变量的值从类中放入表单的变量中
【发布时间】: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 谢谢先生,我已经编辑了我的问题,所以我可以详细解释它。非常感谢。

标签: vb.net class-variables


【解决方案1】:

你还没有学过方法吗?您只需将第二个 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 传入,因为您可以让该方法返回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

【讨论】:

  • 哦,对不起。我之前没有想到。谢谢!先生,您的代码对我有帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多