Update struct in .tag property.
Hey guys can someone take a peek at this code and tell me where I am going wrong. I am creating labels at runtime. Inside the .tag property I have a structure. Everytime I attempt to update the structure the .tag property doesn't update.
Here is the structure:
Code:
Public Structure structChip
Dim kingStatus As Boolean
Dim currentTileLoc As Integer
Dim currX As Integer
Dim currY As Integer
Dim previousTileLoc As Integer
Dim prevX As Integer
Dim prevY As Integer
Dim jumped As Boolean
Dim selected As Boolean
Dim name As String
Dim checkerColor As String
Dim boardSquareColor As String
End Structure
Here is how I am attempting to update it. As I step through it the structure itself is updated but the instance of the structure inside the tag doesn't seem to update. Any ideas?
Code:
Dim checkers As New Windows.Forms.Label
With checkers
.Name = checkArray.Name
.Image = Image.FromFile("..\checkerGreentileRed1.gif")
.AutoSize = False
.Size = New Size(40, 40)
.Parent = frmBoard.picBoard
.Visible = True
.BringToFront()
.Location = New Point(x, y)
.Tag = Nothing
'update the structure
checkStruct.previousTileLoc = checkStruct.currentTileLoc
checkStruct.prevX = checkStruct.currX
checkStruct.prevY = checkStruct.currY
checkStruct.currentTileLoc = tile
checkStruct.currX = x
checkStruct.currY = y
checkStruct.checkerColor = greenWithRed
checkStruct.selected = False
.Tag = checkStruct
End With
Re: Update struct in .tag property.
That second code snippet looks like how you're creating it in the first place, not how you're updating it later.
The issue is almost certainly the fact that you're using a structure, which means that getting the value creates a copy. To update the object in the Tag of the Label you would first need to get it, which creates a copy, then modify that copy and, finally, overwrite the original with the copy by assigning it back to the Tag of the Label. If it was a class you could just do this:
vb.net Code:
DirectCast(myLabel.Tag, SomeClass).SomeProperty = someValue
because classes are reference types. With value types, i.e. structures, you must do this:
vb.net Code:
Dim tag As SomeStructure = DirectCast(myLabel.Tag, SomeStructure)
tag.SomeProperty = someValue
myLabel.Tag = tag
Re: Update struct in .tag property.
Worthy of mentioning that the controls are in an array?
Re: Update struct in .tag property.
Alright J, lemme give that a shot. Not that I doubt the your Kung-Fu.... I doubt my ability!! hahah! I don't know why I do this to myself.
Re: Update struct in .tag property.
Alright tried casting it and it through an exception. I altered the code to the following. From what I can see the structure itself does update. As I step through it I can see the values correctly stored. However, I am not saving them correctly to the tag. I have altered the code to such:
vb Code:
Dim checkers As New Windows.Forms.Label
With checkers
'STRUCT
Dim checkStruct As frmBoard.structChip
checkStruct = frmBoard.checkersArray(count)
.Name = checkArray.Name
.Image = Image.FromFile("..\checkerGreentileRed1.gif")
.AutoSize = False
.Size = New Size(40, 40)
.Parent = frmBoard.picBoard
.Visible = True
.BringToFront()
.Location = New Point(x, y)
.Tag = Nothing
'update the structure
checkStruct.previousTileLoc = checkStruct.currentTileLoc
checkStruct.prevX = checkStruct.currX
checkStruct.prevY = checkStruct.currY
checkStruct.currentTileLoc = tile
checkStruct.currX = x
checkStruct.currY = y
checkStruct.checkerColor = greenWithRed
checkStruct.selected = False
.Tag = checkStruct
End With
Re: Update struct in .tag property.
I always always updated structures the same way, through loops and setting structure vars equal to different values. I just don't understand why it updates but doesn't save in the .tag information of the label.
Re: Update struct in .tag property.
So, if you put a breakpoint on the End With line, what is the value of checkers.Tag?
Re: Update struct in .tag property.
its the correct data. It just seems that when I go through the event added to the label its pulling from the wrong spot (thats what it looks like) so what I think is happening is that I AM updating that structure there BUT I'm not appending it to the structure stored in the .tag. Hold on a sec though I think Im going about this all wrong.
I already have my array of 24 game pieces I don't see a reason why I need to recreate them. I am going to try this by just updated the .location property and moving them. However, that still don't solve my struct update issue. Give me a few minutes to hammer away at this. Maybe I'll find the issue.
Re: Update struct in .tag property.
Quote:
Originally Posted by
Abrium
its the correct data. It just seems that when I go through the event added to the label its pulling from the wrong spot (thats what it looks like) so what I think is happening is that I AM updating that structure there BUT I'm not appending it to the structure stored in the .tag.
I'm afarid that that doesn't make much sense. From your code, my guess is that the problem is that you're getting a copy of your structure from an array, then updating it and assigning it to the Label's Tag, but you aren't pushing your changes back to the array, so the array and the Tag are different.
Re: Update struct in .tag property.
Damn,
You're right, I'm updating... pushing to the piece but I'm not updating the eff'ing array! FAK. lol... Only at midnight do these problems come about. Let me look at that. As soon as I started reading statement I didn't even have to finish it. I need another redbull.
Re: Update struct in .tag property.
Structures have their place but you ALWAYS need to be aware of the fact that whenever you get a structure instance from a field, property or method result, you're actually getting a copy. As such, any changes you made MUST be pushed back to the original if you want them to be in sync. That generally means assigning the updated copy back to the original variable.
Re: Update struct in .tag property.
what time is it over there Jm?
Re: Update struct in .tag property.
Quote:
Originally Posted by
Abrium
what time is it over there Jm?
You posted at 4:38 PM, my time.
Re: Update struct in .tag property.
Alright, I got this to work. It may not be pro but it works. Let me know if you can see any way to improve this that is within my realm of expertise.
So when I create the checkers at runtime I do so with the following code:
vb Code:
'Begin load with White/green with red tiles.
If color = "white" And tile = "red" Then
For counter = 1 To 12
Dim x As Integer
Dim y As Integer
Dim gameTile As Integer
gameTile = LoadRedBottom(counter - 1)
detTileCords(gameTile, x, y)
Dim checkers As New Windows.Forms.Label
With checkers
Dim checkerChip As New frmBoard.structChip
.Name = "checkers" & counter
.Image = Image.FromFile("..\checkerWhitetileRed1.gif")
.AutoSize = False
.Size = New Size(40, 40)
.Parent = frmBoard.picBoard
.Visible = True
.BringToFront()
.Location = New Point(x, y)
checkerChip.kingStatus = False
checkerChip.currentTileLoc = gameTile
checkerChip.currX = x
checkerChip.currY = y
checkerChip.previousTileLoc = 0
checkerChip.prevX = 0
checkerChip.prevY = 0
checkerChip.jumped = False
checkerChip.selected = False
checkerChip.name = checkers.Name
checkerChip.tileColor = "red"
checkerChip.boardPosition = "bottom"
.Tag = checkerChip
End With
AddHandler checkers.MouseClick, AddressOf getMouseCords
frmBoard.checkersArray(counter - 1) = checkers
gameTile = 0
x = 0
y = 0
Next
You were right J, the problem I had was with updating the wrong array but then I realized I didn't need an array of checkers AND an array of the structures that I was using for tags within the checkers. Pretty much what I was doing was updating the struct array and NOT the checker array which meant when I selected that checker through a click event the application was working with incorrect data. I solved this by completely deleting the structure array and calling, changing, and updating the struct inside the control with the following code:
vb Code:
Dim x As Integer
Dim y As Integer
detTileCords(tile, x, y)
Dim tag As New frmBoard.structChip
tag = CType(checker.Tag, frmBoard.structChip)
If tag.kingStatus = True Then
Select Case tile
Case Is = tag.currentTileLoc + 7
checker.Location = New Point(x, y)
Case Is = tag.currentTileLoc + 9
checker.Location = New Point(x, y)
Case Is = tag.currentTileLoc - 7
checker.Location = New Point(x, y)
Case Is = tag.currentTileLoc - 9
checker.Location = New Point(x, y)
End Select
ElseIf tag.kingStatus = False Then
If tag.boardPosition = "bottom" Then
Select tile
Case Is = tag.currentTileLoc - 7
checker.Location = New Point(x, y)
Select Case tile
Case Is = 1
tag.kingStatus = True
checker.Image = Image.FromFile("..\checkerGreentileRed1.gif")
Case Is = 2
tag.kingStatus = True
Case Is = 3
tag.kingStatus = True
Case Is = 4
tag.kingStatus = True
Case Is = 5
tag.kingStatus = True
Case Is = 6
tag.kingStatus = True
Case Is = 7
tag.kingStatus = True
Case Is = 8
tag.kingStatus = True
End Select
Case Is = tag.currentTileLoc - 9
checker.Location = New Point(x, y)
Select Case tile
Case Is = 1
tag.kingStatus = True
Case Is = 2
tag.kingStatus = True
Case Is = 3
tag.kingStatus = True
Case Is = 4
tag.kingStatus = True
Case Is = 5
tag.kingStatus = True
Case Is = 6
tag.kingStatus = True
Case Is = 7
tag.kingStatus = True
Case Is = 8
tag.kingStatus = True
End Select
Case Else
MsgBox("Improper move: moveChip - kingStatus = false")
End Select
ElseIf tag.boardPosition = "top" Then
Select tile
Case Is = tag.currentTileLoc + 7
checker.Location = New Point(x, y)
Select Case tile
Case Is = 57
tag.kingStatus = True
Case Is = 58
tag.kingStatus = True
Case Is = 59
tag.kingStatus = True
Case Is = 60
tag.kingStatus = True
Case Is = 61
tag.kingStatus = True
Case Is = 62
tag.kingStatus = True
Case Is = 63
tag.kingStatus = True
Case Is = 64
tag.kingStatus = True
End Select
Case Is = tag.currentTileLoc + 9
checker.Location = New Point(x, y)
Select Case tile
Case Is = 57
tag.kingStatus = True
Case Is = 58
tag.kingStatus = True
Case Is = 59
tag.kingStatus = True
Case Is = 60
tag.kingStatus = True
Case Is = 61
tag.kingStatus = True
Case Is = 62
tag.kingStatus = True
Case Is = 63
tag.kingStatus = True
Case Is = 64
tag.kingStatus = True
End Select
Case Else
End Select
Else
MsgBox("Improper move - bottom moveChip")
End If
End If
With tag
.previousTileLoc = .currentTileLoc
.prevX = .currX
.prevY = .currY
.currentTileLoc = tile
.currX = x
.currY = y
.selected = False
End With
checker.Tag = tag
frmBoard.checkSelected = False
After a shitload of case statements I update the structure and then copy it into the .tag property.
Re: Update struct in .tag property.
What's with all those Case Is = X parts? They are all contiguous, they all appear to be integers, and they all set to the same value. Wouldn't it be easier to just write something like:
Code:
If tile > 1 AndAlso tile < 9 then
tag.KingStatus = True
End If
Re: Update struct in .tag property.
It sure would. These are things that I don't think of while I'm doing them. I had already went back and done that. Good looking out though.