473,498 Members | 2,018 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.AddrBook 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

Jul 17 '05 #1
2 5344

"Paul" <pa***@mindless.com> wrote in message
news:42********@news.iprimus.com.au...
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"


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).First = "Joe"
AddrArray(1).Last = "Bloggs"
AddrArray(1).Phone = "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.
Jul 17 '05 #2
On Mon, 11 Apr 2005 23:28:36 +1000, "Paul" <pa***@mindless.com> wrote:
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


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
Jul 17 '05 #3

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

Similar topics

16
1785
by: Roland Hall | last post by:
In working with arrays, I have found that I am unable to dimension and array with a variable that has an integer value but I can redimension one this way. I haven't see any information that tells...
28
20270
by: Daniel | last post by:
Hello =) I have an object which contains a method that should execute every x ms. I can use setInterval inside the object construct like this - self.setInterval('ObjectName.methodName()',...
14
4313
by: Randell D. | last post by:
Folks, Here's my problem: I am createing an array in a file using the reslut of some PHP and MySQL processing. An example of my array would be examlpe="example one"; examlpe="example...
4
11961
by: please-answer-here | last post by:
I want to store a bunch of textstrings (about 500) returned from a database query in a session array. I've googled all over the net, not to find any working method. Is there anybody who has the...
3
1138
by: Sugapablo | last post by:
Ok, here's my dumbdumb question of the day: I have this loop that's supposed to loop through the 11 XML elements named "title" in the object. When I put a number in the array brackets like...
4
2326
by: Deniz Bahar | last post by:
Hello all, Often times programs in C have arrays used as buffers and shared among different sections of code. The need arises to have position indicators to point to different parts of an array...
11
2939
by: ctman770 | last post by:
Hi Everyone, Is it faster to save the precise location of an html dom node into a variable in js, or to use getElementById everytime you need to access the node? I want to make my application...
29
3619
by: shuisheng | last post by:
Dear All, The problem of choosing pointer or reference is always confusing me. Would you please give me some suggestion on it. I appreciate your kind help. For example, I'd like to convert a...
7
3391
by: arnuld | last post by:
this programme gives unusual output. i am not able to find out where the semantic bug lies: /* C++ Primer - 4/e * * an example from section section 7.2.4, page 241 * STATEMENT * write a...
0
6993
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
7162
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,...
0
7197
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7375
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5456
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4584
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3088
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
650
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.