469,275 Members | 1,871 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,275 developers. It's quick & easy.

2-D Array

I've just moved onto 2-D Arrays in my VB6 beginners book. I'm struggling to
get my head around the following problem but no doubt you will think it
rather easy.
My program is supposed to allow up to 20 students to enter their name and 3
exam marks, with each students details being entered into a 2-D Array upon
the click of a command button. There is also a list box and another command
button which will display the contents of the array. I have next to nothing
done but I'll post it anyway. Thank you.

Option Explicit
Dim Details(1 To 20, 1 To 3) As String
Dim Index As Integer

Private Sub cmdAddToArray_Click()
Index = Index + 1
Details(Index, 1) = txtMark1.Text
Details(Index, 2) = txtMark2.Text
Details(Index, 3) = txtMark3.Text
End Sub
Jul 17 '05 #1
3 15376
"Roy Riddex" <ro**************@blueyonder.co.uk> wrote in message
news:Zf*****************@news-binary.blueyonder.co.uk...
I've just moved onto 2-D Arrays in my VB6 beginners book. I'm struggling to get my head around the following problem but no doubt you will think it
rather easy.
My program is supposed to allow up to 20 students to enter their name and 3 exam marks, with each students details being entered into a 2-D Array upon
the click of a command button. There is also a list box and another command button which will display the contents of the array. I have next to nothing done but I'll post it anyway. Thank you.

Option Explicit
Dim Details(1 To 20, 1 To 3) As String
Dim Index As Integer

Private Sub cmdAddToArray_Click()
Index = Index + 1
Details(Index, 1) = txtMark1.Text
Details(Index, 2) = txtMark2.Text
Details(Index, 3) = txtMark3.Text
End Sub


Where are you going to store the student's name?
....
Dim Details(1 To 20, 1 To 4) As String
....
How are you going to store the student's name?
....
Private Sub cmdAddToArray_Click()
Index = Index + 1
Details(Index, 0) = txtStudentName.Text
Details(Index, 1) = txtMark1.Text
....

And a question for you:
What happens when Index >20?
Should get you started.
Geoff Turner
Jul 17 '05 #2

Where are you going to store the student's name?
That's why I'm struggling. The book says to store the student's name and the
3 exam marks in the 2D array. It also says I must declare it as a string
since all values must be of the same type within an array. So it looks as
tho my 1 to 20 statement may be wrong, but, what do I replace it with?
And a question for you:
What happens when Index >20?


I haven't thought that far ahead yet because I really feel I've hit a brick
wall. Maybe my brain has hit it's limits with programming...lol. Can
somebody point me in the right direction?
Thanks
Jul 17 '05 #3
I believe you may find it advantageous to use named constants for the
variables in the array ... it can make identifying different elements easier
when reading the code. And get into the habit of using 0-based arrays -- a
suggestion.

For example ...

private const nStudentName = 0
private const nMark1 = 1
private const nMark2 =2
private const nMark3 = 3

Dim Details(0 To 19, 0 To 3) As String '20 x 4

Details(Index, nStudentName ) = txtName.Text
Details(Index, nMark1) = txtMark1.Text
Details(Index, nMark2) = txtMark2.Text
Details(Index, nMark3) = txtMark3.Text
--

Randy Birch
MVP Visual Basic
http://vbnet.mvps.org/
Please respond only to the newsgroups so all can benefit.
"Roy Riddex" <ro**************@blueyonder.co.uk> wrote in message
news:Zf*****************@news-binary.blueyonder.co.uk...
: I've just moved onto 2-D Arrays in my VB6 beginners book. I'm struggling
to
: get my head around the following problem but no doubt you will think it
: rather easy.
: My program is supposed to allow up to 20 students to enter their name and
3
: exam marks, with each students details being entered into a 2-D Array upon
: the click of a command button. There is also a list box and another
command
: button which will display the contents of the array. I have next to
nothing
: done but I'll post it anyway. Thank you.
:
: Option Explicit
: Dim Details(1 To 20, 1 To 3) As String
: Dim Index As Integer
:
: Private Sub cmdAddToArray_Click()
: Index = Index + 1
: Details(Index, 1) = txtMark1.Text
: Details(Index, 2) = txtMark2.Text
: Details(Index, 3) = txtMark3.Text
: End Sub
:
:
Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Brian | last post: by
2 posts views Thread by Stormkid | last post: by
8 posts views Thread by vcardillo | last post: by
12 posts views Thread by Sam Collett | last post: by
8 posts views Thread by Mike S. Nowostawsky | last post: by
104 posts views Thread by Leszek | last post: by
7 posts views Thread by Jim Carlock | last post: by
17 posts views Thread by =?Utf-8?B?U2hhcm9u?= | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.