Search code examples
vb.netclass-variables

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


I have a class names intersection_regular_island and I created a new instance of it in form1. I wanted to re-declare a certain variable in that class so I tried this code

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

Whereas if I do it like this, it is working

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

Note that it is just a small part of the code, it's not the actual code. It is for illustration purposes so maybe I might missed something.

Seems like I'm missing something that is so simple?

Edit:

I want to do it like that so I can easily perform a select case

So instead of copying and pasting this code many times

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

I wanted it become like this

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

Solution

  • Have you not learned about methods yet? You simply put that second Select Case into a method:

    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
    

    and call that method for the first 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
    

    In fact, you wouldn't even have to pass the arrowWidth in because you could just have that method return a Color and then create Pen outside:

    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