Using variables to reference Array elements

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

    #1

    Using variables to reference Array elements

    Please help, I am working on an Active-x project involving Arrays with up
    to
    20 elements populated in a class module. In order to keep programming and
    debugging simple, Instead of refering to each element with numbers, would it
    be correct to refer to each element with constants as per the example shown
    bellow.

    Option Explicit
    Dim AddrArray(1 To 20, 1 To 3)
    Dim Address As clsMyClass
    Dim MyStr As String
    Dim intX As Integer
    Const First = 1
    Const Last = 2
    Const Phone = 3

    Sub Main()
    Set Address = New clsMyClass
    Address.AddrBoo k First, Last, Phone, AddrArray()
    For intX = 1 To 4
    MsgBox AddrArray(intX, First) & vbCr & AddrArray(intX, Last) & vbCr &
    AddrArray(intX, Phone)
    Next intX
    End Sub

    '' with this in a class module called clsMyClass

    Public Function AddrBook(First, Last, Phone, AddrArray())
    AddrArray(1, First) = "Joe"
    AddrArray(1, Last) = "Bloggs"
    AddrArray(1, Phone) = "5551234"

    AddrArray(2, First) = "Sarah"
    AddrArray(2, Last) = "Bloggs"
    AddrArray(2, Phone) = "5554321"

    AddrArray(3, First) = "John"
    AddrArray(3, Last) = "Doh"
    AddrArray(3, Phone) = "5551111"

    AddrArray(4, First) = "Jane"
    AddrArray(4, Last) = "Doh"
    AddrArray(4, Phone) = "5554444"
    End Function

    '' Please note: hard-coded for simplicity

    If there is a simpler way of doing this, I would greatly appreciate any
    help,

    Thanks in advance

    Paul



  • Steve Gerrard

    #2
    Re: Using variables to reference Array elements


    "Paul" <paulp@mindless .com> wrote in message
    news:425a7b88_1 @news.iprimus.c om.au...[color=blue]
    > Please help, I am working on an Active-x project involving Arrays with up to
    > 20 elements populated in a class module. In order to keep programming and
    > debugging simple, Instead of refering to each element with numbers, would it
    > be correct to refer to each element with constants as per the example shown
    > bellow.
    >
    > Dim AddrArray(1 To 20, 1 To 3)
    > Dim Address As clsMyClass
    > Dim MyStr As String
    > Dim intX As Integer
    > Const First = 1
    > Const Last = 2
    > Const Phone = 3
    >
    > '' with this in a class module called clsMyClass
    >
    > Public Function AddrBook(First, Last, Phone, AddrArray())
    > AddrArray(1, First) = "Joe"
    > AddrArray(1, Last) = "Bloggs"
    > AddrArray(1, Phone) = "5551234"
    >
    > AddrArray(2, First) = "Sarah"
    > AddrArray(2, Last) = "Bloggs"
    > AddrArray(2, Phone) = "5554321"
    >[/color]

    Nothing wrong with using constants to reference array elements.

    Another approach is a user defined type:
    Public Type Addr
    First As String
    Last As String
    Phone As String
    End Type
    Dim AddrArray(1 To 20) As Addr
    ......
    AddrArray(1).Fi rst = "Joe"
    AddrArray(1).La st = "Bloggs"
    AddrArray(1).Ph one = "5551234"
    ....
    I'm not sure why AddrArray is not *inside* of clsMyClass, and why you are not
    explicitly typing it as strings, but that is another topic.


    Comment

    • J French

      #3
      Re: Using variables to reference Array elements

      On Mon, 11 Apr 2005 23:28:36 +1000, "Paul" <paulp@mindless .com> wrote:
      [color=blue]
      >Please help, I am working on an Active-x project involving Arrays with up
      >to
      >20 elements populated in a class module. In order to keep programming and
      >debugging simple, Instead of refering to each element with numbers, would it
      >be correct to refer to each element with constants as per the example shown
      >bellow.
      >
      >Option Explicit
      >Dim AddrArray(1 To 20, 1 To 3)
      >Dim Address As clsMyClass
      >Dim MyStr As String
      >Dim intX As Integer
      >Const First = 1
      >Const Last = 2
      >Const Phone = 3[/color]

      It makes life easier when you have to open up the code a year later

      You might be interested in going one step further and using an Enum


      Comment

      Working...