Vb.net 对象引用未设置为对象的实例。visualbasicvb

Vb.net 对象引用未设置为对象的实例。visualbasicvb,vb.net,visual-studio-2010,genetic-algorithm,Vb.net,Visual Studio 2010,Genetic Algorithm,java中的相同代码在我用VB已经有一段时间了,但我认为New-方法中的DIM-语句创建了一个新的局部变量tours,它隐藏了全局变量tours 试试这个: 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 A

java中的相同代码在

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

试试这个:

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

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

试试这个:

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
试试看

试试看


请注意,数组的大小将为populationSize+1,因为在VB中,数组声明中传递的值是上限,而不是大小。请注意,数组的大小将为populationSize+1,因为在VB中,数组声明中传递的值是上限,而不是大小。
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