Question Linking Forms And Arrays

Topthai

New member
Joined
May 29, 2010
Messages
4
Programming Experience
Beginner
Hey Guys,

I am new to vb.net. I am currently programming Space Invaders. Firstly i have two forms What is code for linking form 1 to form 2?

Secondly i have coded and tried to understand arrays. I currently Have
VB.NET:
Private Sub tmrUFO_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrUFO.Tick
        Randomize()
        Dim WhichEnemy As Integer = Int(Rnd() * 6) ' random number 0-5
        Enemy(WhichEnemy).Top += 4 ' move 4 pixels down
        If Enemy(WhichEnemy).Top > Me.Height Then ' gone off bottom of form
            Enemy(WhichEnemy).Top = -Enemy(WhichEnemy).Height ' send to top of form
        End If
It is telling me there is an error with the line Enemy(WhichEnemy).Top += 4
It is saying the object reference is not set to an instance of an object???
There is a form load code which goes which this code.
VB.NET:
 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim obj As Object, I As Integer = 0, J As Integer = 0
        ' all controls on the form
        If TypeOf obj Is PictureBox Then ' only check for pictureboxes
            If obj.Tag = "spaceship" Then
                Enemy(I) = obj ' add this object to the Enemy array
                I += 1 ' advance ready for next element
            End If
        End If
Again have no idea what is wrong. Just trying to make enemy's move with arrays instead of single programming. Please Help :D
 
Last edited by a moderator:
First things first: Don't put Randomize inside a method that gets executed more than once. Randomize initialises the VB random number generator. Initialisation implies once at startup, not over and over. If you were going to use Randomize then you put it in the Load event handler.

That said, don't use Randomize and Rnd anyway. VB.NET has been around since 2002 and people are still clinging to the VB6 way. In VB.NET, create a single Random object and then call its Next method to get a random Integer:
VB.NET:
Private ranNumGen As New Random

Private Sub tmrUFO_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrUFO.Tick
    Dim WhichEnemy As Integer = Me.ranNumGen.Next(6)
As for the issue, if you know that there are going to be six enemies then why do you need that conditional code in the Load event handler? Why can't you just create the array explicitly:
VB.NET:
Me.Enemy = New PictureBox() {Me.PictureBox1, ... , Me.PictureBox6}
Also, something that contains multiple enemies would more aptly be named "enemies", whereas "enemy" would be an apt name for a single enemy.
 
So off the random integer, create the multiple enemy's which are in the array? Also how would movement be created with the array. So all these enemies move together.
 
Im not understanding how or what the code does. Im new and trying to get my head around arrays for this assignment. So the code i gave first how do i change that to suit the suggestion you offered????
 
Back
Top