Question

I'm trying to make a matching game in Visual Basic 2010, and to check if the chosen cards are in fact a match I want to compare the value of firstchoice.image to secondchoice.image.

The problem I am running into is that in the comparison the value of anything.image is returning as System.Drawing.Bitmap, therefore the comparison always returns true.

The comparison code as of right now is:

If FirstI Is My.Resources.circle Then
    TempOne = 1
ElseIf FirstI Is My.Resources.crescent Then
    TempOne = 2
ElseIf FirstI Is My.Resources.pentagram Then
    TempOne = 3
ElseIf FirstI Is My.Resources.square Then
    TempOne = 4
ElseIf FirstI Is My.Resources.triangle Then
    TempOne = 5
ElseIf FirstI Is My.Resources.yinyang Then
    TempOne = 6
End If

If SecondI Is My.Resources.circle Then
    TempOne = 1
ElseIf SecondI Is My.Resources.crescent Then
    TempOne = 2
ElseIf SecondI Is My.Resources.pentagram Then
    TempOne = 3
ElseIf SecondI Is My.Resources.square Then
    TempOne = 4
ElseIf SecondI Is My.Resources.triangle Then
    TempOne = 5
ElseIf SecondI Is My.Resources.yinyang Then
    TempOne = 6
End If
Debug.WriteLine(FirstI)
Debug.WriteLine(SecondI)
If TempOne = TempTwo Then
    Return True
Else
    Return False
End If

FirstI and SecondI are declared as

Private FirstI As Image
Private SecondI As Image

and are filled in with data pulled from an array of images.

Private Cards() As Image = {My.Resources.circle, My.Resources.crescent, My.Resources.pentagram, My.Resources.square, My.Resources.triangle, My.Resources.yinyang, My.Resources.circle, My.Resources.crescent, My.Resources.pentagram, My.Resources.square, My.Resources.triangle, My.Resources.yinyang}
Was it helpful?

Solution

If I were you I wouldn't try to directly compare objects/values, I would simply compare state. Keep a FirstIIndex and SecondIIndex (as Integer) that are index integers from 0 to Cards.Length. When you set that value, set the image toi Cards(FirstIIndex). If you want to see if they are set to the same thing, just check to see if FirstIIndex = SecondIndex.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top