I have tried making instances of forms, but none of them have worked.
This code works and turns the "lamp" on and off:
Public Class Lamp
' declare field
Private lampColor As Color
Public Sub New()
' initialize field
lampColor = MainForm.lampShape.FillColor
End Sub ' New
Public Sub Switch()
' determine if lamp is on or off
If lampColor = Color.Silver Then
' turn on lamp
lampColor = Color.Yellow
Else
' turn off lamp
lampColor = Color.Silver
End If
' display color on lamp
MainForm.lampShape.FillColor = lampColor
End Sub ' Switch
End Class
This code does not work:
Public Class Lamp
' declare fields
Private lampColor As Color
Private main As New MainForm
Public Sub New()
' initialize field
lampColor = main.lampShape.FillColor
End Sub ' New
Public Sub Switch()
' determine if lamp is on or off
If lampColor = Color.Silver Then
' turn on lamp
lampColor = Color.Yellow
Else
' turn off lamp
lampColor = Color.Silver
End If
' display color on lamp
main.lampShape.FillColor = lampColor
End Sub ' Switch
End Class
I have tried this with many other projects too and none of them work.