Question

Whenever I try to operate on a variable, it instantly gets set to false. This method is supposed to turn the full property on the box object stored in box_list to True. It finds the correct boxes and changes the property, but whenever it is accessed again, it turns to false.

Public Sub DefineFilled()
    Dim ship As New Ship(Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing)
    Dim x As Integer = 0
    Dim y As Integer = 0

    ship = AirCraftCarrier
    If ship.has_moved Then
        Debug.Print("")
        For i As Integer = 0 To ship.length - 1
            x = ship.space_filled(i, 0) 'space_filled is a list that stores each point that the ship takes up on the grid
            y = ship.space_filled(i, 1) 
            PlayerBoard.box_list(x, y).full = True 'Sets variable to True

            Debug.Print(PlayerBoard.box_list(x, y).full) 'Prints variable as False
        Next
    End If
End Sub

Edit 1: PlayerBoard definition

(In Main)

Public PlayerBoard As Board

Private Sub Main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    PlayerBoard = New Board(tile_size, 330, Color.Silver)
End Sub

(Board class)

Public Class Board
    Dim _box_list(,) as Box
    Public Property box_list() As Box(,)
        Get
            Return _box_list
        End Get
        Set(ByVal value As Box(,))
            _box_list = value
        End Set
    End Property

    Public Sub New(ByVal pos_x As Integer, ByVal pos_y As Integer, ByVal colourp As Color)
        ReDim _box_list(10, 10)
    End Sub

Edit 2: How box_list is defined (In board class)

Private Sub BuildBoard()

    For y As Integer = 0 To 9
        For x As Integer = 0 To 9
            Dim box As New Box(x, y, New PictureBox)
            With box
                .image.Location = New Point(start_location(0) + x * tile_size, start_location(1) + y * tile_size)
                .image.Size = New Size(tile_size, tile_size)
                .image.BackColor = colour
                .image.BorderStyle = BorderStyle.FixedSingle
            End With
            box_list(x, y) = box
            Main.Controls.Add(box_list(x, y).image)
        Next
    Next

End Sub

Edit 3: Definition of box

Public Class Box
    Dim _image As PictureBox
    Public Property image() As PictureBox
        Get
            Return _image
        End Get
        Set(ByVal value As PictureBox)
            _image = value
        End Set
    End Property
    Dim _full As Boolean
    Public Property full() As Boolean
        Get
            Return _full
        End Get
        Set(ByVal value As Boolean)
            _full = full
        End Set
    End Property

    Public Sub New(ByVal location_x As Integer, ByVal location_y As Integer, ByVal imagep As PictureBox)
        image = imagep
    End Sub

End Class
Était-ce utile?

La solution

Your problem with this code is in the full property on the Box class.

You have it written like this now:

Public Property full() As Boolean
    Get
        Return _full
    End Get
    Set(ByVal value As Boolean)
        _full = full
    End Set
End Property

It should be this:

Public Property full() As Boolean
    Get
        Return _full
    End Get
    Set(ByVal value As Boolean)
        _full = value
    End Set
End Property

Your current code is recursively setting the property back to its own value. Hence it is staying false.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top