lundi 16 avril 2018

Visual Basic 2010 - Random number selection process works in blank forms, but does not function in this specific form of my project

I'm working on a project, so there are quite a few conditions specifically based on variables from other forms in my project. However, I have tested these variables using blank labels, and the variables themselves are loading into the form.

The raw code, with comments on areas I specifically have a problem with, can be found below.

Public Class Form5
Dim gen As System.Random
Dim count As Integer = 0
Public goblin1Count As Integer = 0
Public goblin2Count As Integer = 0
Public goblin3Count As Integer = 0
Public goblin4Count As Integer = 0
Public goblinTypeform5 As Integer = Form4.goblinTypeform4

Private Sub Form5_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Timer1.Enabled = True
    Dim picChoose As Integer
    Dim test As Integer               'This is a pretty standard random number selector that should place the number in a blank dummy label.
    test = gen.Next(1, 50)            'It doesn't work.
    Label1.Text = test
    If goblinTypeform5 < 4 Then   'Tested this variable with a blank label, it loaded so the condition (goblintype) to start the code below should be met
        picChoose = gen.Next(1, 9)
        If picChoose = 1 Then
            goblinPic.Image = My.Resources.goblin1
            goblinPic.SizeMode = PictureBoxSizeMode.StretchImage
        ElseIf picChoose = 2 Then
            goblinPic.Image = My.Resources.goblin2
            goblinPic.SizeMode = PictureBoxSizeMode.StretchImage
        ElseIf picChoose = 3 Then
            goblinPic.Image = My.Resources.goblin3
            goblinPic.SizeMode = PictureBoxSizeMode.StretchImage
        ElseIf picChoose = 4 Then
            goblinPic.Image = My.Resources.goblin4
            goblinPic.SizeMode = PictureBoxSizeMode.StretchImage
        ElseIf picChoose = 5 Then
            goblinPic.Image = My.Resources.goblin6
            goblinPic.SizeMode = PictureBoxSizeMode.StretchImage
        ElseIf picChoose = 7 Then
            goblinPic.Image = My.Resources.goblin7
            goblinPic.SizeMode = PictureBoxSizeMode.StretchImage
        ElseIf picChoose = 8 Then
            goblinPic.Image = My.Resources.goblin8
            goblinPic.SizeMode = PictureBoxSizeMode.StretchImage
        End If
    ElseIf goblinTypeform5 = 4 Then
        goblinPic.Image = My.Resources.goblinEX
        goblinPic.SizeMode = PictureBoxSizeMode.StretchImage
    End If
    Dim random As Integer = gen.Next(1, 11)
    If goblinTypeform5 = 1 Then
        Dim array() As String = {"cat", "fat", "hat", "bat", "lad", "bad", "dab", "mad", "sad", "ape"}    
        wordLabel.Text = array(random)                                                                    'Tested this section of code in a completely blank form,
        While count < 3                                                                                   ' worked perfectly.
            If attackBox.Text = array(random) Then
                goblin1Count = goblin1Count + 1
                Me.Hide()
            End If
        End While
        MessageBox.Show("You Died.")
        'close after this
    ElseIf goblinTypeform5 = 2 Then
        Dim array() As String = {"duck", "suck", "luck", "bear", "star", "fair", "fire", "dire", "slap", "flap"}
        wordLabel.Text = array(random)
        While count < 3
            If attackBox.Text = array(random) Then
                goblin2Count = goblin2Count + 1
                Me.Hide()
            End If
        End While
        MessageBox.Show("You Died.")       'this messagebox does not appear, even when the timer reaches above 3 seconds
        'close after this
    ElseIf goblinTypeform5 = 3 Then
        Dim array() As String = {"stare", "flare", "grape", "spare", "rifle", "build", "sword", "spear", "beard", "power"}
        wordLabel.Text = array(random)
        While count < 3
            If attackBox.Text = array(random) Then
                goblin3Count = goblin3Count + 1
                Me.Hide()
            End If
        End While
        MessageBox.Show("You Died.")
        'close after this
    ElseIf goblinTypeform5 = 4 Then
        Dim array() As String = {"sahelanthropus", "ardipithecus", "australopithecus", "paranthropus", "kenyanthropus", "rudolfensis", "heidelbergensis", "neanderthalensis", "aethiopicus", "tchadensis"}
        wordLabel.Text = array(random)
        While count < 5
            If attackBox.Text = array(random) Then
                goblin4Count = goblin4Count + 1
                Me.Hide()
            End If
        End While
        MessageBox.Show("You Died.")
        'close after this
    End If
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    count = count + 1
End Sub
End Class

If I were to narrow it down to just the snippets that work in other forms but not this one, it would look like this:

Dim test As Integer               
    test = gen.Next(1, 50)            
    Label1.Text = test


picChoose = gen.Next(1, 9)
        If picChoose = 1 Then
            goblinPic.Image = My.Resources.goblin1
            goblinPic.SizeMode = PictureBoxSizeMode.StretchImage


Dim random As Integer = gen.Next(1, 11)
        Dim array() As String = {"cat", "fat", "hat", "bat", "lad", "bad", "dab", "mad", "sad", "ape"}    
        wordLabel.Text = array(random)

Again, these seem like fairly simple processes, but they do not work when placed in the context of my project's code. There are no errors or warnings identified by visual basic, and as I have mentioned the variables seem to carry over to the form as intended, so the conditions stipulated should likewise be met without issue.

It seems worthwhile to mention that, though this does not prevent the project from starting or running, these messages do appear:

A first chance exception of type 'System.InvalidOperationException' occurred in GOBLIN SLAYER.exe
A first chance exception of type 'System.NullReferenceException' occurred in GOBLIN SLAYER.exe

These messages are repeated a ridiculously large amount of times.

To further clarify, the GUI/form itself loads (blank), but the pictures do not appear in the picture box, the word does not appear in the wordLabel, and the messagebox activated by 3 seconds passing does not activate.

I have a feeling it has to do with the order the code is placed in, but I am not sure how else to organize it.




Aucun commentaire:

Enregistrer un commentaire