Global Array未保存对其所做的更改

时间:2016-03-04 13:42:28

标签: vb.net

Public Sub changePoints(ByVal Name As String, StartPoint As Boolean, Point As Integer)

    Locations(Point).Name = Name

    If StartPoint = True Then
        For i = 0 To 19
            Locations(i).StartPoint = False
        Next
    End If
    Locations(Point).StartPoint = StartPoint
    DrawGraph()
End Sub

Private Sub SaveButton_Click(sender As Object, e As EventArgs) Handles SaveButton.Click

    GraphicalPlot.changePoints(PointNameBox.Text, MakeStart.Checked, LoadedPointValue)

End Sub

所以我有2个表格。第一个表单,当单击选定的点时,会在可编辑的文本框中显示第二个表单,其中包含该点的详细信息。当您单击第二个窗体上的保存按钮以保存详细信息时,上面的第二个子例程将运行,并以包含全局变量的第一个窗体形式调用子例程。

信息正确传递给第一种形式的子程序。当我在更改后立即检查全局数组的值(在行位置(点).Name = Name之后)时,它表示该值已正确更改。

然而,在子例程完成后,全局数组返回到调用子例程之前的值,并且没有输入到第二个表单的信息的跟踪。

如果有帮助,全局数组的结构为:

Structure EnteredPoint
    Dim Xcoord As Integer   'Stores X co-ordinate of point
    Dim Ycoord As Integer   'Store Y co-ordinate of point
    Dim Name As String      'Stores name of point, used to check if point is unused
    Dim StartPoint As Boolean   'Checks if the point is the start point
    Dim Selected As Boolean     'Checks if the point is currently being hovered over the mouse
    Dim nextPoint As Integer    'Used to implement a linked list, making deleting and recreating points easier
End Structure

这个名称可以改回原来应该是不可能的;我的程序唯一一次在初始化之外更改名称(将其设置为"未使用"或已创建的点数)。

我用Google搜索了它,向我的老师和朋友展示了它,尽管玩了好几个小时我们仍然找不到任何东西。任何帮助都将非常感谢,因为这是我的Comp4课程!

编辑:为vbnet3d添加

Sub DrawGraph()
    'Used to draw the current state.
    G = Me.CreateGraphics
    G.Clear(Color.White)    'Sets entire background to white
    Dim placeholder As Integer = 0  'Used to store the current point being checked.
    If UsedLocations > 0 Then   'This part will only run if any points have been made
        Do Until Locations(placeholder).nextPoint = 0   'Loops until all points have been drawn

            If Locations(placeholder).StartPoint = True Then    'will only draw this if it is the starting point
                G.FillEllipse(Brushes.LightBlue, Locations(placeholder).Xcoord - 3, Locations(placeholder).Ycoord - 3, 16, 16)
            End If
            If Locations(placeholder).Selected = True Then      'Will only draw this if it is the currently selected point
                G.FillEllipse(Brushes.LightGreen, Locations(placeholder).Xcoord - 3, Locations(placeholder).Ycoord - 3, 16, 16)
            End If
            'Draws the actual Point
            G.FillEllipse(Brushes.Black, Locations(placeholder).Xcoord, Locations(placeholder).Ycoord, 10, 10)
            If UsedLocations <= 20 Then
                placeholder = Locations(placeholder).nextPoint  'Gets the next point to be checked.
            End If


        Loop
        If UsedLocations = 20 Then
            If Locations(placeholder).Selected = True Then      'Will only draw this if it is the currently selected point
                G.FillEllipse(Brushes.LightGreen, Locations(placeholder).Xcoord - 3, Locations(placeholder).Ycoord - 3, 16, 16)
            End If
            'Draws the actual Point
            G.FillEllipse(Brushes.Black, Locations(placeholder).Xcoord, Locations(placeholder).Ycoord, 10, 10)
        End If
    End If

End Sub

2 个答案:

答案 0 :(得分:1)

问题是您在数组中使用值类型(结构)。当您访问数组元素时,您将获得该项目的副本,因此您的更改仅在副本上进行。

您必须从数组中获取EnteredPoint,根据需要更改属性,然后将该点重新分配回数组:

Public Sub changePoints(ByVal Name As String, StartPoint As Boolean, Point As Integer)

    Dim pointToChange As EnteredPoint = Locations(Point)

    pointToChange.Name = Name

    If StartPoint = True Then
        For i = 0 To 19
            Dim tmp As EnteredPoint = Locations(i)
            tmp.StartPoint = False
            Locations(i) = tmp
        Next
    End If

    pointToChange.StartPoint = StartPoint
    Locations(Point) = pointToChange

    DrawGraph()
End Sub

如果您希望保留原始语法,则可以将EnteredPoint更改为类,它将按预期工作。

答案 1 :(得分:0)

您的问题是由默认表单引用引起的。 (参见@Plutonix的评论)这意味着你有一个名为&#34; GraphicalPlot&#34;的基类实例。但你也有一个活跃的实例&#34; GraphicalPlot&#34;这是一个单独的实例。这两个类的形式相同,但不共享内存数据(一个是另一个的模型)。当您进行以下调用时,它会引用模型表单而不是活动表单:

Private Sub SaveButton_Click(sender As Object, e As EventArgs) Handles SaveButton.Click

    GraphicalPlot.changePoints(PointNameBox.Text, MakeStart.Checked, LoadedPointValue)

End Sub

您需要创建对活动表单(GraphicalPlot)的引用,并使用 Me 作为当前实例引用将其传递给GraphicsMenu。有点像:

GraphicsMenu.Load(Me, PointName, PointNum)

在GraphicsMenu中:

Dim GP As GraphicalPlot
Public Sub Load(ByVal PlotForm As GraphicalPlot, ByVal Name As String, pointnumber As Integer)

    GP = PlotForm

End Sub