0
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中的相同代码在此链接中

4

2 回答 2

2

我做过 VB 已经有一段时间了,但我认为您DIM在 -method 中的 -statement创建了一个隐藏 global variableNew的新局部变量。tourstours

试试这个:

Public Class Population

 Dim tours() As Tour

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

    tours = 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 
End Sub
于 2013-02-23T12:29:32.463 回答
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
于 2013-02-23T12:53:47.047 回答