|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Hello everyone,
First of all let me thank you for reading my post and thank you in advance for any replies I may recieve. I have been learning to program in VB now for the last few months and I have got the hang of all of it quickly except one feature of programming and programming in VB specifically. I don't understand the use of an Array? Can anyone explain arrays to me in a straight forward manner and thier use. |
|
#2
|
|||
|
|||
|
ikea,
An array is simply a storage mechanism similar to a variable but more powerful. In other words, you would use a variable like this: firstname = "Wil" What if you wanted to store your full name but in separate variables? firstname = "Wil" lastname = "Moore" suffix = "III" ok, simple right. Pretty clean isn't it? Lets mess it up a bit. Suppose you wanted to store the full names of three different people concurrently (all at the same time as opposed to using the variable once, then writing over it with a new name). With variables you would do something like this: person1_firstname = "Wil" person1_lastname = "Moore" person1_suffix = "III" person2_firstname = "Ian" person2_lastname = "Dunn" person2_suffix = "" Not too bad you might think right? Well, that is wrong. Because there are not too many real applications where you would know how many people you are going to have to account for, and even more rarely will you be hard coding this anyhow. Usually you won't know the amount of names to be entered so you have to make the application flexible. Its kind of like having an open bar. You could just go buy 5 cases of beer in hopes that you have estimated how many people will show up to the party and drink... or you could just have an open bar and make sure everyone has enough drinks regardless of how many people show up. An array is just that. You initialize and use it as in the following: Dim aryNames(3) As String aryNames(0) = "Wil" aryNames(1) = "Moore" aryNames(2) = "III" That is an array of the simplest form. In order to use the values of an array you will have to call each item by its index. For example: debug.print aryNames(0) & space(1) & aryNames(1) & space(1) & aryNames(2) An array is indexed by number by default. The above array has these Indexes: 0, 1, 2 That is a total of three items, but notice our index numbers go only to 2. Arrays start with an index of zero, thus, the highest index (we call this the array's upper bound) is Its length minus one... UpperBound = Length - 1 In this case the length is three, so its upper bound is: 3 - 1 = 2 An array has a lower bound of 0 by default. Some programming languages allow you to change an array's lower bound such as Visual basic. You may be thinking this is enough to get you through all your programming tasks... maybe... but you should know that you can create multidimensional arrays as well as dynamic arrays. A dynamic array is most useful when you need to let the application figure out how many indexes you need instead of worrying about how many you might need. This way you don't create an array that has an upper bound that is way more than what you need, or one that is too small. You wouldn't want to write an application where you anticipated the array's size may grow to 2000, but that would not happen for months of usage. Why waste all that space initially when you could dynamically allocate your space. I won't go into multidimensional arrays or dynamic arrays here, but here is a sample of each: Multidimensional Array: Dim aryNames(2,3) As String aryNames(0,0) = "Wil" ... ... aryNames(1,0) = "Ian" . . . Dynamic Array: Redim aryNames(0) As String aryNames(UBound(aryNames)) = "Wil" Redim Preserve aryNames(UBound(aryNames) + 1) aryNames(UBound(aryNames)) = "Moore" . . . Hope this helps
__________________
__________________________________________________ _ Wil Moore III, MCP | Integrations Specialist | Senior Consultant Are You Listed...? | DigitallySmooth Inc. Last edited by laidbak : June 15th, 2003 at 01:25 AM. |
|
#3
|
||||
|
||||
|
Nicely put Wil... i didn't know where to start with that one... esp multi-dimensionals!
|
|
#4
|
|||
|
|||
|
Thanks
|
|
#5
|
|||
|
|||
|
Sweeeet.
Thanks a lot for you help Wil. I understand how arrays work a lot better now. I hopped over to your website and took a look at the wedding pictures. It looked pretty straight. Congrats. URL Marcus |
|
#6
|
|||
|
|||
|
Marcus,
No problem on the array info... I'm glad it helped. Thanks for checking out the wedding photos and the congrats. |
![]() |
| Viewing: Dev Articles Community Forums > Programming > ASP Development > Problems Grasping function of Arrays |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|