Results 1 to 16 of 16

Thread: Update struct in .tag property.

  1. #1

    Thread Starter
    Addicted Member Abrium's Avatar
    Join Date
    Feb 2007
    Location
    The Great State of Texas
    Posts
    205

    Exclamation 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
    Abrium
    Asking the beginners questions so you don't have to!
    If by chance hell actually froze over and I some how helped you... Please rate.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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:
    1. DirectCast(myLabel.Tag, SomeClass).SomeProperty = someValue
    because classes are reference types. With value types, i.e. structures, you must do this:
    vb.net Code:
    1. Dim tag As SomeStructure = DirectCast(myLabel.Tag, SomeStructure)
    2.  
    3. tag.SomeProperty = someValue
    4. myLabel.Tag = tag
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member Abrium's Avatar
    Join Date
    Feb 2007
    Location
    The Great State of Texas
    Posts
    205

    Re: Update struct in .tag property.

    Worthy of mentioning that the controls are in an array?
    Abrium
    Asking the beginners questions so you don't have to!
    If by chance hell actually froze over and I some how helped you... Please rate.

  4. #4

    Thread Starter
    Addicted Member Abrium's Avatar
    Join Date
    Feb 2007
    Location
    The Great State of Texas
    Posts
    205

    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.
    Abrium
    Asking the beginners questions so you don't have to!
    If by chance hell actually froze over and I some how helped you... Please rate.

  5. #5

    Thread Starter
    Addicted Member Abrium's Avatar
    Join Date
    Feb 2007
    Location
    The Great State of Texas
    Posts
    205

    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:
    1. Dim checkers As New Windows.Forms.Label
    2.                 With checkers
    3.                     'STRUCT
    4.                     Dim checkStruct As frmBoard.structChip
    5.                     checkStruct = frmBoard.checkersArray(count)
    6.  
    7.                     .Name = checkArray.Name
    8.                     .Image = Image.FromFile("..\checkerGreentileRed1.gif")
    9.                     .AutoSize = False
    10.                     .Size = New Size(40, 40)
    11.                     .Parent = frmBoard.picBoard
    12.                     .Visible = True
    13.                     .BringToFront()
    14.                     .Location = New Point(x, y)
    15.                     .Tag = Nothing
    16.                     'update the structure
    17.                     checkStruct.previousTileLoc = checkStruct.currentTileLoc
    18.                     checkStruct.prevX = checkStruct.currX
    19.                     checkStruct.prevY = checkStruct.currY
    20.                     checkStruct.currentTileLoc = tile
    21.                     checkStruct.currX = x
    22.                     checkStruct.currY = y
    23.                     checkStruct.checkerColor = greenWithRed
    24.                     checkStruct.selected = False
    25.                     .Tag = checkStruct
    26.                 End With
    Abrium
    Asking the beginners questions so you don't have to!
    If by chance hell actually froze over and I some how helped you... Please rate.

  6. #6

    Thread Starter
    Addicted Member Abrium's Avatar
    Join Date
    Feb 2007
    Location
    The Great State of Texas
    Posts
    205

    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.
    Abrium
    Asking the beginners questions so you don't have to!
    If by chance hell actually froze over and I some how helped you... Please rate.

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Update struct in .tag property.

    So, if you put a breakpoint on the End With line, what is the value of checkers.Tag?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Addicted Member Abrium's Avatar
    Join Date
    Feb 2007
    Location
    The Great State of Texas
    Posts
    205

    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.
    Abrium
    Asking the beginners questions so you don't have to!
    If by chance hell actually froze over and I some how helped you... Please rate.

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Update struct in .tag property.

    Quote Originally Posted by Abrium View Post
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10

    Thread Starter
    Addicted Member Abrium's Avatar
    Join Date
    Feb 2007
    Location
    The Great State of Texas
    Posts
    205

    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.
    Abrium
    Asking the beginners questions so you don't have to!
    If by chance hell actually froze over and I some how helped you... Please rate.

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  12. #12

    Thread Starter
    Addicted Member Abrium's Avatar
    Join Date
    Feb 2007
    Location
    The Great State of Texas
    Posts
    205

    Re: Update struct in .tag property.

    what time is it over there Jm?
    Abrium
    Asking the beginners questions so you don't have to!
    If by chance hell actually froze over and I some how helped you... Please rate.

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Update struct in .tag property.

    Quote Originally Posted by Abrium View Post
    what time is it over there Jm?
    You posted at 4:38 PM, my time.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  14. #14

    Thread Starter
    Addicted Member Abrium's Avatar
    Join Date
    Feb 2007
    Location
    The Great State of Texas
    Posts
    205

    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:
    1. 'Begin load with White/green with red tiles.
    2.         If color = "white" And tile = "red" Then
    3.             For counter = 1 To 12
    4.                 Dim x As Integer
    5.                 Dim y As Integer
    6.                 Dim gameTile As Integer
    7.                 gameTile = LoadRedBottom(counter - 1)
    8.                 detTileCords(gameTile, x, y)
    9.                 Dim checkers As New Windows.Forms.Label
    10.                 With checkers
    11.                     Dim checkerChip As New frmBoard.structChip
    12.                     .Name = "checkers" & counter
    13.                     .Image = Image.FromFile("..\checkerWhitetileRed1.gif")
    14.                     .AutoSize = False
    15.                     .Size = New Size(40, 40)
    16.                     .Parent = frmBoard.picBoard
    17.                     .Visible = True
    18.                     .BringToFront()
    19.                     .Location = New Point(x, y)
    20.                     checkerChip.kingStatus = False
    21.                     checkerChip.currentTileLoc = gameTile
    22.                     checkerChip.currX = x
    23.                     checkerChip.currY = y
    24.                     checkerChip.previousTileLoc = 0
    25.                     checkerChip.prevX = 0
    26.                     checkerChip.prevY = 0
    27.                     checkerChip.jumped = False
    28.                     checkerChip.selected = False
    29.                     checkerChip.name = checkers.Name
    30.                     checkerChip.tileColor = "red"
    31.                     checkerChip.boardPosition = "bottom"
    32.                     .Tag = checkerChip
    33.                 End With
    34.                 AddHandler checkers.MouseClick, AddressOf getMouseCords
    35.                 frmBoard.checkersArray(counter - 1) = checkers
    36.                 gameTile = 0
    37.                 x = 0
    38.                 y = 0
    39.             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:
    1. Dim x As Integer
    2.         Dim y As Integer
    3.         detTileCords(tile, x, y)
    4.         Dim tag As New frmBoard.structChip
    5.         tag = CType(checker.Tag, frmBoard.structChip)
    6.  
    7.  
    8.  
    9.         If tag.kingStatus = True Then
    10.             Select Case tile
    11.                 Case Is = tag.currentTileLoc + 7
    12.                     checker.Location = New Point(x, y)
    13.                 Case Is = tag.currentTileLoc + 9
    14.                     checker.Location = New Point(x, y)
    15.                 Case Is = tag.currentTileLoc - 7
    16.                     checker.Location = New Point(x, y)
    17.                 Case Is = tag.currentTileLoc - 9
    18.                     checker.Location = New Point(x, y)
    19.             End Select
    20.         ElseIf tag.kingStatus = False Then
    21.             If tag.boardPosition = "bottom" Then
    22.                  Select tile
    23.                     Case Is = tag.currentTileLoc - 7
    24.                         checker.Location = New Point(x, y)
    25.                         Select Case tile
    26.                             Case Is = 1
    27.                                 tag.kingStatus = True
    28.  
    29.                                 checker.Image = Image.FromFile("..\checkerGreentileRed1.gif")
    30.                             Case Is = 2
    31.                                 tag.kingStatus = True
    32.                             Case Is = 3
    33.                                 tag.kingStatus = True
    34.                             Case Is = 4
    35.                                 tag.kingStatus = True
    36.                             Case Is = 5
    37.                                 tag.kingStatus = True
    38.                             Case Is = 6
    39.                                 tag.kingStatus = True
    40.                             Case Is = 7
    41.                                 tag.kingStatus = True
    42.                             Case Is = 8
    43.                                 tag.kingStatus = True
    44.                         End Select
    45.                     Case Is = tag.currentTileLoc - 9
    46.                         checker.Location = New Point(x, y)
    47.                         Select Case tile
    48.                             Case Is = 1
    49.                                 tag.kingStatus = True
    50.                             Case Is = 2
    51.                                 tag.kingStatus = True
    52.                             Case Is = 3
    53.                                 tag.kingStatus = True
    54.                             Case Is = 4
    55.                                 tag.kingStatus = True
    56.                             Case Is = 5
    57.                                 tag.kingStatus = True
    58.                             Case Is = 6
    59.                                 tag.kingStatus = True
    60.                             Case Is = 7
    61.                                 tag.kingStatus = True
    62.                             Case Is = 8
    63.                                 tag.kingStatus = True
    64.                         End Select
    65.                     Case Else
    66.                         MsgBox("Improper move: moveChip - kingStatus = false")
    67.                 End Select
    68.             ElseIf tag.boardPosition = "top" Then
    69.                             Select tile
    70.                     Case Is = tag.currentTileLoc + 7
    71.                         checker.Location = New Point(x, y)
    72.                         Select Case tile
    73.                             Case Is = 57
    74.                                 tag.kingStatus = True
    75.                             Case Is = 58
    76.                                 tag.kingStatus = True
    77.                             Case Is = 59
    78.                                 tag.kingStatus = True
    79.                             Case Is = 60
    80.                                 tag.kingStatus = True
    81.                             Case Is = 61
    82.                                 tag.kingStatus = True
    83.                             Case Is = 62
    84.                                 tag.kingStatus = True
    85.                             Case Is = 63
    86.                                 tag.kingStatus = True
    87.                             Case Is = 64
    88.                                 tag.kingStatus = True
    89.                         End Select
    90.                     Case Is = tag.currentTileLoc + 9
    91.                         checker.Location = New Point(x, y)
    92.                         Select Case tile
    93.                             Case Is = 57
    94.                                 tag.kingStatus = True
    95.                             Case Is = 58
    96.                                 tag.kingStatus = True
    97.                             Case Is = 59
    98.                                 tag.kingStatus = True
    99.                             Case Is = 60
    100.                                 tag.kingStatus = True
    101.                             Case Is = 61
    102.                                 tag.kingStatus = True
    103.                             Case Is = 62
    104.                                 tag.kingStatus = True
    105.                             Case Is = 63
    106.                                 tag.kingStatus = True
    107.                             Case Is = 64
    108.                                 tag.kingStatus = True
    109.                         End Select
    110.                     Case Else
    111.                 End Select
    112.             Else
    113.                 MsgBox("Improper move - bottom moveChip")
    114.             End If
    115.  
    116.         End If
    117.         With tag
    118.             .previousTileLoc = .currentTileLoc
    119.             .prevX = .currX
    120.             .prevY = .currY
    121.             .currentTileLoc = tile
    122.             .currX = x
    123.             .currY = y
    124.             .selected = False
    125.         End With
    126.         checker.Tag = tag
    127.         frmBoard.checkSelected = False

    After a shitload of case statements I update the structure and then copy it into the .tag property.
    Abrium
    Asking the beginners questions so you don't have to!
    If by chance hell actually froze over and I some how helped you... Please rate.

  15. #15
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    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
    My usual boring signature: Nothing

  16. #16

    Thread Starter
    Addicted Member Abrium's Avatar
    Join Date
    Feb 2007
    Location
    The Great State of Texas
    Posts
    205

    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.
    Abrium
    Asking the beginners questions so you don't have to!
    If by chance hell actually froze over and I some how helped you... Please rate.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width