Array problem?

  • Thread starter Thread starter Lisa
  • Start date Start date
L

Lisa

Can anybody tell me what's the problem with this code?

Class CRate
public mRate as double
public mValue as double
end class

Class CBox
public bNo as long
public bRate as CRate()
End Class

======================

Private Sub Test()
Dim J as long
Dim BoxRead as CBox = New CBox(9) {}

For i = 0 to 9
BoxRead(i).bNo = i
BoxRead(i).bRate = New CRate(5) {}
For j = 0 to 5
BoxRead(i).bRate(j).mRate = (i * 12) + j
BoxRead(i).bRate(j).mValue = (i * j)
next j

next i

For i = 0 to 9
For j = 0 to 5
msgbox (BoxRead(i).bRate(j).mRate ) '<=== Error ?!
BoxRead(i).bRate(j).mValue
next j
next i

End Sub
 
Lisa,

* "Lisa said:
Can anybody tell me what's the problem with this code?

Class CRate
public mRate as double
public mValue as double
end class

Class CBox
public bNo as long
public bRate as CRate()
End Class

======================

Private Sub Test()
Dim J as long
Dim BoxRead as CBox = New CBox(9) {}

For i = 0 to 9
BoxRead(i).bNo = i
BoxRead(i).bRate = New CRate(5) {}
For j = 0 to 5
BoxRead(i).bRate(j).mRate = (i * 12) + j
BoxRead(i).bRate(j).mValue = (i * j)
next j

next i

For i = 0 to 9
For j = 0 to 5
msgbox (BoxRead(i).bRate(j).mRate ) '<=== Error ?!
BoxRead(i).bRate(j).mValue
next j
next i

End Sub

I am not sure what doesn't work (when does the error occur, which error,
....), but try this code:

\\\
Class CRate
Public mRate As Double
Public mValue As Double
End Class

Class CBox
Public bNo As Long
Public bRate() As CRate
End Class

Private Sub Test()
Dim i As Integer, j As Integer
Dim BoxRead() As CBox = New CBox(9) {}
For i = 0 To 9
BoxRead(i) = New CBox
BoxRead(i).bNo = i
BoxRead(i).bRate = New CRate(5) {}
For j = 0 To 5
BoxRead(i).bRate(j) = New CRate
BoxRead(i).bRate(j).mRate = (i * 12) + j
BoxRead(i).bRate(j).mValue = (i * j)
Next j
Next i
For i = 0 To 9
For j = 0 To 5
MsgBox(BoxRead(i).bRate(j).mRate)
MsgBox(BoxRead(i).bRate(j).mValue)
Next j
Next i
End Sub
///
 
Lisa,
In addition to Herfried's comments, I would consider adding some
constructors, such as:

Class CRate
Public mRate As Double
Public mValue As Double

Public Sub New(ByVal rate As Double, ByVal value As Double)
mRate = rate
mValue = value
End Sub

End Class

Class CBox
Public bNo As Long
Public bRate As CRate()

Public Sub New(ByVal no As Long)
bNo = no
bRate = New CRate(5) {}

Dim j As Integer
For j = 0 To 5
bRate(j) = New CRate((no * 12) + j, (no * j))
Next j
End Sub

End Class

Private Sub Test()
Dim i, j As Integer
Dim BoxRead() As CBox = New CBox(9) {}

For i = 0 To 9
BoxRead(i) = New CBox(i)
Next i

For i = 0 To 9
For j = 0 To 5
MessageBox.Show(BoxRead(i).bRate(j).mRate) '<=== Error ?!
MessageBox.Show(BoxRead(i).bRate(j).mValue)
Next j
Next i

End Sub

Remember that creating an array of a Class:
Dim BoxRead as CBox = New CBox(9) {}

Initializes each element to Nothing, you need to explicitly intialize each
element with a new element.

Hope this helps
Jay
 
Hi Lisa,

Let me suggest that you set Option Strict on. There are a number of things
wrong.

You've declared variable "J" as a long but it is assumed to be an integer in
the code. I guess that is because the values 0 through 5 are integer
values. You haven't defined variable "I" at all.

When you are outputting the values one is displayed in a MsgBox but the
other isn't... it's just an expression.

The big item appears to be that you create an array of objects but you never
instantiate the CBox and CRate objects.

And unless there is some advantage for you the declaration for BoxRead can
be simplified to:

Dim BoxRead(9) as CBox

Oh... and I realize there might be more happening but you might make CRate
and CBox structures.

Tom
 
Here it is using structures... no constructors necessary. Note that I added
two constants to represent the size of the I and J arrays. That way you can
set them higher or lower without trying to locate all the references to 9
and 5 in your loops.


Structure CBox
Public bNo As Long
Public bRate() As CRate
End Structure

Structure CRate
Public mRate As Double
Public mValue As Double
End Structure

Private Sub Test()

Const MaxI As Integer = 9
Const MaxJ As Integer = 5

Dim BoxRead(MaxI) As CBox

Dim i, j As Integer

For i = 0 To MaxI

BoxRead(i).bNo = i
ReDim BoxRead(i).bRate(MaxJ)

For j = 0 To MaxJ
BoxRead(i).bRate(j).mRate = (i * 12) + j
BoxRead(i).bRate(j).mValue = (i * j)
Next j

Next i

For i = 0 To MaxI
For j = 0 To MaxJ
Console.WriteLine(i.ToString & ":" & j.ToString _
& " " & BoxRead(i).bRate(j).mRate.ToString _
& " - " & BoxRead(i).bRate(j).mValue.ToString)
Next j
Next i

End Sub
 
Tom,
Option Explicit On would be good (for her) also, as you stated she did not
have "I" defined at all.

Jay
 
Back
Top