【问题标题】:Boolean being set to False whenever it is accessed again?布尔值在再次访问时设置为 False?
【发布时间】:2014-07-03 06:16:03
【问题描述】:

每当我尝试对变量进行操作时,它都会立即设置为 false。此方法应该将存储在box_list 中的box 对象上的full 属性设置为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

【问题讨论】:

  • 我们需要看看PlayerBoard是什么类型的定义,尤其是box_list函数。
  • 已添加。希望我得到了所有相关的东西。
  • 附带问题:去掉 box_list() 属性的 Set 选项。你不想要它。删除 Set 后,您仍然可以更改该数组中的元素。
  • 还有:status_norepro。我使用此代码构建的示例程序按预期工作。还有一些你没有向我们展示的东西......也许是重新声明你的对象实例。
  • @alexanderd5398 您仍然可以在没有 Set 的情况下更改数组中的元素。您唯一不能做的就是在一个作业中替换整个数组。例如,这在没有设置器的情况下仍然可以工作:PlayerBoard.box_list(x, y).full = True。这也将是:PlayerBoard.box_list(x, y) = New Box()。两者都只使用 Get 选项。但这不起作用:PlayerBoard.box_list = New Box(9,9)。理解为什么会这样将大大有助于您了解对象的真正工作原理。

标签: arrays vb.net list boolean


【解决方案1】:

此代码的问题在于Box 类的full 属性。

你现在是这样写的:

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

【讨论】:

  • 非常感谢!我不敢相信我错过了:/
  • 没问题。有时是小事让我们大吃一惊。
猜你喜欢
  • 2020-06-23
  • 1970-01-01
  • 2017-05-24
  • 1970-01-01
  • 2016-06-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多