每当我尝试操作变量时,它立即被设置为false。此方法应该将full
中存储的box
对象的box_list
属性设置为True。它找到正确的框并更改属性,但无论何时再次访问它,它都会变为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
编辑1:PlayerBoard定义
(主要)
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
(董事会课程)
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
编辑2:如何定义box_list(在板类中)
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
编辑3:框的定义
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
答案 0 :(得分:2)
此代码的问题在full
类的Box
属性中。
你现在就这样写了:
Public Property full() As Boolean
Get
Return _full
End Get
Set(ByVal value As Boolean)
_full = full
End Set
End Property
应该是这样的:
Public Property full() As Boolean
Get
Return _full
End Get
Set(ByVal value As Boolean)
_full = value
End Set
End Property
您当前的代码以递归方式将属性设置回其自己的值。因此它会留下false
。