2-D Array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Roy Riddex

    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_C lick()
    Index = Index + 1
    Details(Index, 1) = txtMark1.Text
    Details(Index, 2) = txtMark2.Text
    Details(Index, 3) = txtMark3.Text
    End Sub


  • Geoff Turner

    #2
    Re: 2-D Array

    "Roy Riddex" <roy_riddexNOSP AM@blueyonder.c o.uk> wrote in message
    news:ZfaLb.6664 $d27.2105@news-binary.blueyond er.co.uk...[color=blue]
    > I've just moved onto 2-D Arrays in my VB6 beginners book. I'm struggling[/color]
    to[color=blue]
    > 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[/color]
    3[color=blue]
    > 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[/color]
    command[color=blue]
    > button which will display the contents of the array. I have next to[/color]
    nothing[color=blue]
    > 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_C lick()
    > Index = Index + 1
    > Details(Index, 1) = txtMark1.Text
    > Details(Index, 2) = txtMark2.Text
    > Details(Index, 3) = txtMark3.Text
    > End Sub
    >
    >[/color]

    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_C lick()
    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


    Comment

    • Roy Riddex

      #3
      Re: 2-D Array


      [color=blue]
      > Where are you going to store the student's name?[/color]

      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?
      [color=blue]
      > And a question for you:
      > What happens when Index >20?[/color]

      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...l ol. Can
      somebody point me in the right direction?
      Thanks


      Comment

      • Randy Birch

        #4
        Re: 2-D Array

        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

        Please respond only to the newsgroups so all can benefit.


        "Roy Riddex" <roy_riddexNOSP AM@blueyonder.c o.uk> wrote in message
        news:ZfaLb.6664 $d27.2105@news-binary.blueyond er.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_C lick()
        : Index = Index + 1
        : Details(Index, 1) = txtMark1.Text
        : Details(Index, 2) = txtMark2.Text
        : Details(Index, 3) = txtMark3.Text
        : End Sub
        :
        :


        Comment

        Working...