Reputation: 95
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
same code in java is in this link
Upvotes: 0
Views: 1267
Reputation: 11484
Try,
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
Upvotes: 1
Reputation: 2571
It's been a while that I've done VB but I think your DIM
-statement in the New
-method creates a new local variable tours
that hides the global variable tours
.
Try this:
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
Upvotes: 2