Question

It seems to be a simple question, but I am having a hard time learning it.

I have a VB.NET Windows Forms Application. I want to create a few objects of the airplane class when the application loads, and then store them in an array so that other subs and functions can work with them. However, I can only set this array to private and therefore I can't work with it later anymore.

I am creating the objects like this:

'create objects on load
Public Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
    myUnits.Text = 0

    Dim plane(4) As airplane
    plane(0) = New airplane("he112", 1, "", 10, 10, 0, 0, 100)
    plane(1) = New airplane("bf109", 1, "", 10, 10, 0, 0, 100)
    plane(2) = New airplane("stuka", 1, "", 10, 10, 0, 0, 100)
    plane(3) = New airplane("do217", 1, "", 10, 10, 0, 0, 100)

    startnewgame()
    plane(0).researchPlane()
End Sub

And then I want to use the array later, like this:

Sub refreshIndicators()
    dayIndicator.Text = "Day " & dayNumber
    For Each unit As airplane In plane
        If (unit.isResearched And unit.isFriendly = True And unit.getAmount > 0) Then
            myUnits.Text = myUnits.Text & unit.getName & " " & unit.getAmount & " x" & vbCrLf
        End If
    Next
End Sub

I get the following error:

"plane" is not declared or can't be accessed.

But I can't set "plane" to public. Also, do you think it's good practice to use an array in the Load event handler like that?

Was it helpful?

Solution

The problem is the scope of your plane variable. Currently, you are declaring it as a local variable inside the Form1_Load method. By doing so, that makes it inaccessible from any other method anywhere in your application. Also, since you never set any other variable (with a greater scope) to the object, that array object will get cleaned up at the garbage collector's earliest convenience and the array will cease to exist. The way to correct it is to declare the variable as a field of your class rather than as a local variable to a method. When it is declared as a field to the class, it will be within the scope of all of the methods in the class. For instance:

Public Class Form1
    Private plane(4) As airplane  ' This is a field of the class, scoped to all methods in the class

    Public Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        plane(0) = New airplane("he112", 1, "", 10, 10, 0, 0, 100)
        ' ...
    End Sub
End Class

For more information on scope. See this article in the MSDN. Also, this article looks applicable too.

I don't think there is anything particularly wrong with doing that work in the Load event handler. As long as it doesn't take a long time to execute it, I would say that is a very reasonable place to put it. If it did take a long time to run that code, for some reason, it would be better to do it in a background worker which is started by the Shown event so that you don't slow down the showing of the form.

OTHER TIPS

You have defined the variable airplane in the Form1_Load methode so it is only accessible from it. If you want to have an access of it from another methods you need to delcare it outside of Form1_Load:

Dim plane(4) As airplane

'create objects on load
Public Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top