473,387 Members | 1,700 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Array problem?

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
Nov 20 '05 #1
5 909
Lisa,

* "Lisa" <tc*@pc.jaring.my> scripsit:
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
///

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #2
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

"Lisa" <tc*@pc.jaring.my> wrote in message
news:uF**************@tk2msftngp13.phx.gbl... 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

Nov 20 '05 #3
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
"Lisa" <tc*@pc.jaring.my> wrote in message
news:uF**************@tk2msftngp13.phx.gbl...
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

Nov 20 '05 #4
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
Nov 20 '05 #5
Tom,
Option Explicit On would be good (for her) also, as you stated she did not
have "I" defined at all.

Jay

"Tom Leylan" <ge*@iamtiredofspam.com> wrote in message
news:e7**************@TK2MSFTNGP10.phx.gbl...
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
"Lisa" <tc*@pc.jaring.my> wrote in message
news:uF**************@tk2msftngp13.phx.gbl...
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


Nov 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Federico G. Babelis | last post by:
Hi All: I have this line of code, but the syntax check in VB.NET 2003 and also in VB.NET 2005 Beta 2 shows as unknown: Dim local4 As Byte Fixed(local4 = AddressOf dest(offset)) ...
9
by: justanotherguy63 | last post by:
Hi, I am designing an application where to preserve the hierachy and for code substitability, I need to pass an array of derived class object in place of an array of base class object. Since I...
5
by: ritchie | last post by:
Hi, I am writing to ask if anyone can see why my array is not being sorted correctly? It's an array of 4 elements(ints 1,2,3,4) but after calling the selection sort it comes back sorted as...
8
by: Gerald | last post by:
I have a problem with an array of pointers. In a program I'm writing, I have to read a file, containing thousands of short lines. The content of another file will be compared against each line...
12
by: arkobose | last post by:
my earlier post titled: "How to input strings of any lengths into arrays of type: char *array ?" seems to have created a confusion. therefore i paraphrase my problem below. consider the...
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
8
by: intrepid_dw | last post by:
Hello, all. I've created a C# dll that contains, among other things, two functions dealing with byte arrays. The first is a function that returns a byte array, and the other is intended to...
104
by: Leszek | last post by:
Hi. Is it possible in javascript to operate on an array without knowing how mamy elements it has? What i want to do is sending an array to a script, and this script should add all values from...
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
4
by: assgar | last post by:
Hi Seasons Greetings Its back, I am being haunted. I thought I had resolved this problem but I am intermittently the receving the warnings below. This code consist of a 1) HTML form, 2)...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.