Brandontom Brandontom - 4 days ago 5
Vb.net Question

If statements and picture box values issue

i'm trying to create a snap game but when it comes to checking if the picture box contains the correct image, it simply just does not work, i've done a bit of research regarding this and implemented the ideas. it does not throw up any sort of error but i just do not receive a increased value when i should. Please have a look at this code and tell me if you know where i'm going wrong.

Attempt 1:

Dim BirdPics() As Image = {My.Resources.Image_1}
If tbxAnimal_Group.Text = "Birds" And BirdPics.Contains(pbxPicture.Image) Then

CurrentPoints += 1
lblScore.Text = "Score:" & CurrentPoints
End If


Attempt 2

Dim BirdPics() As Image = {My.Resources.Image_1}
If tbxAnimal_Group.Text = "Birds" And pbxPicture Is BirdPics Then

CurrentPoints += 1
lblScore.Text = "Score:" & CurrentPoints
End If

Answer

Dont use "BirdPics.Contains"; it does not work like that.

Simple comparisons cannot be made on images.

IF ThisImage = ThatImage THEN ' doesn't necessarily work even though it will compile.

Create a structure or class and store them in a list.

Structure gameObjects
 BirdPic as Image
 BirdPicName as string
End Structure

DIM GamePictures as NEW List(of gameObjects)

SUB Main()

    for index = 0 to gamepictures.count - 1
        If tbxAnimal_Group.Text = "Birds" AND GamePictures(index).BirdPicName = "BlueJay" THEN
            ' Do Something
        End If
    next
END SUB 
Comments