0

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.

Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
Logan
  • 926
  • 1
  • 10
  • 18
  • the first block doesnt create an instance at all. in the second, it is hard to tell what you are trying to do. what is `lampShape` in the form? Clarifying what `none of them work` means might be helpful too – Ňɏssa Pøngjǣrdenlarp Dec 18 '15 at 18:12
  • I know, the first I just use the class name. And I made an application that turns the color of a square from silver to yellow, like a lamp. When I use MainForm, the colors change. When I use the main instance, the colors do not change. – Logan Dec 18 '15 at 18:16
  • The first uses what is known as the default instance. It is a creepy VB-ism where VB creates a form instance with the same name as the class. In the second, you never SHOW the form, so you could very well be working with 2 form instances: one you see and one your class created (depends on the code we cant see) `main As New MainForm` creates a NEW form instance independant of one you might see on screen – Ňɏssa Pøngjǣrdenlarp Dec 18 '15 at 18:19
  • I'm just trying to use an instance I create to manipulate the controls on the form. – Logan Dec 18 '15 at 18:21
  • Well, since your class never SHOWS the main it created, yet you say you can see the "square" not change color, you very likely have 2 instances of the form being used. Add `main.Show()` to your constructor (sub New) and I bet you'll see a second form - which works with the lamp – Ňɏssa Pøngjǣrdenlarp Dec 18 '15 at 18:23
  • After I did that, two forms are now opened – Logan Dec 18 '15 at 18:27

1 Answers1

2

Since your class never SHOWS the main it created, yet you say you can see the "square" not change color, you very likely have 2 instances of the form in use: the one that the VB app framework creates as a Startup form, then the one you create in the Lamp class. Add main.Show() to your constructor (sub New) and I bet you'll see a second form.

From comments:

After I did that, two forms are now opened

The keyword New creates a new object. So, while your second class does use a form instance, it created its own New one which is a different one than the one VB created and showed.


Assuming that form is the app's main form, and the form creates the class, this is what you want:

Public Class Lamp
    Private lampColor As Color
    Private main As MainForm           ' no New!

    Public Sub New(frm As MainForm)
        main = frm                     ' store reference to the form passed

        lampColor = main.lampShape.FillColor
    End Sub ' New
    ...

Then when you create it:

Public Class MainForm
   Private myLamp As Lamp            ' just declare it
   ...
   ' in form load:
       myLamp = New Lamp(Me)             ' pass this form inst to the lamp object
Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
  • I have a MainForm and I want that to be my main class. I just want to be able to do things like fill labels on the MainForm through another class. Is there any way to do that? Because Dim main As New MainForm does not accomplish it. – Logan Dec 19 '15 at 23:04
  • Read the answer carefully - it does not use `Dim main As New MainForm` in fact it explicitly says ***not*** to use `New` and shows how to pass the existing form instance to the lamp class – Ňɏssa Pøngjǣrdenlarp Dec 19 '15 at 23:22