你调用的对象是空的。视觉基础vb

时间:2013-02-23 12:11:24

标签: vb.net visual-studio-2010 genetic-algorithm

Public Class Population

 Dim tours() As Tour    ' Tour is a class and I have to make and object array

 Public Sub New(ByVal populationSize As Integer, ByVal initialise As Boolean)

    Dim tours As New Tour(populationSize)   ' 

    If initialise Then
        ' Loop and create individuals
        For i As Integer = 0 To (populationSize - 1)
            Dim newTour As New Tour()
            newTour.generateIndividual()
            saveTour(i, newTour)
        Next i
    End If
End Sub

Public Sub saveTour(ByVal index As Integer, ByVal tour As Tour)
    tours(index) = tour           ' getting error in this line 
End Sub

java中的相同代码位于this link

2 个答案:

答案 0 :(得分:2)

我已经做了一段时间VB,但我认为DIM - 方法中的New - 语句创建了一个新的局部变量tours,它隐藏了全局变量{{ 1}}。

试试这个:

tours

答案 1 :(得分:1)

尝试,

Public Sub New(ByVal populationSize As Integer, ByVal initialise As Boolean)

    ReDim tours(populationSize)

    If initialise Then
        ' Loop and create individuals
        For i As Integer = 0 To (populationSize - 1)
            Dim newTour As New Tour()
            newTour.generateIndividual()
            saveTour(i, newTour)
        Next i
    End If
End Sub