Valhalla Legends Forums Archive | Visual Basic Programming | Array tutorial

AuthorMessageTime
Networks
I know nothing about arrays and how to use them can anyone please give me a quick array tutorial and how to use them, declares, and what ever else.

Do not refer me to google, I know how you love it.
April 25, 2004, 6:20 PM
Grok
[quote author=Networks link=board=31;threadid=6472;start=0#msg56863 date=1082917223]
I know nothing about arrays and how to use them can anyone please give me a quick array tutorial and how to use them, declares, and what ever else.

Do not refer me to google, I know how you love it.
[/quote]

Sure can. Think of a post office. There are lots of mailboxes there, but which is which? The boxes are numbered sequentially. That is an array of post office boxes.

An array is a grouping of storage variables. To be an array, one name must be able to access any of the items. To differentiate between one item and any other, you use an index. Like at the post office, all boxes are "P.O. Box" but yours might be #768. Thus, POBox[768], or in VB, POBox(768).

Now that we're on VB ... to create an array of 10 elements ...

Dim POBox(9) As Long

creates an array of ten long variables, numbered POBox(0) to POBox(9).

Understand so far?

Each "POBox" is called an array "element".
The number of each POBox is called its "index".

So using index 7, you get to the 8th element (remember the first index is #0).

MsgBox POBox(7) & " is the 8th element.", vbInformation

You can read or write to the elements, using the proper data types.

POBox(7) = 314159

April 25, 2004, 6:57 PM
Networks
Now can you help impliment this into something confusing and advanced so I become an expert. Teach ME!
April 25, 2004, 7:01 PM
Grok
Is this a joke to see how much time of someone's you can waste today? heh.
April 25, 2004, 7:05 PM
Networks
No, I really want to learn how to use them. Like how to make flag based database or what not. I don't wanna soure steal ;)
April 26, 2004, 12:27 AM
The-FooL
[quote author=Networks link=board=31;threadid=6472;start=0#msg56925 date=1082939275]
No, I really want to learn how to use them. Like how to make flag based database or what not. I don't wanna soure steal ;)
[/quote]

Step 1: Create a userdefined type for the users(with a var for username, access, flags, and whatever else you want.)
Step 2: Create an array of those types
Step 3: Manipulate the array as you see fit.

If you want any furthur detail, goto the library and getyourself a VB book and doit yourself.
April 26, 2004, 1:11 AM
Networks
I have one but its explains a lot more about controls rather than actual code.
April 26, 2004, 6:48 PM
LoRd
[quote author=Networks link=board=31;threadid=6472;start=0#msg57024 date=1083005326]
I have one but its explains a lot more about controls rather than actual code.
[/quote]

... All the code you need:

[quote author=Grok link=board=31;threadid=6472;start=0#msg56873 date=1082919421]
Dim POBox(9) As Long
[/quote]
April 26, 2004, 7:27 PM
Flame
Don't complain if parts are incorrect, this is just the basics, and I'm just coming with it from the top of my head, if you want more in-depth descriptions (going against your first post) try google search: Dynamic Arrays

Dynamic Arrays 101, (out of pure boredom) Enjoy.

If you plan on making a database of some sort as you stated, you may want to use a Dynamic Array. Instead of doing:
[code]Dim POBox(9) As Long[/code]
You would do:
[code]Dim POBox() As Long[/code]
Before the arrays is initialized (on Form_Load, or perhaps before anything is done concerning the array) do:
[code]ReDim POBox(0)[/code]
This will clear all items in the array (if any are present) and reset the array to a lower bound of 0. If you want more information on using ReDim try MSDN, that's where I learned how it was done.

Each time you add an item, you need to expand the arrays index by however many (if the array is going to be small, you can expand it by one, for your purpose expanding by one would be fine) in the following matter:
[code]ReDim Preserve POBox(0 to UBound(POBox) + 1)[/code]
This will expand the array index by one while preserving all items in the array allowing you to set the upper bound item to whatever you please.

Finally, removing from an array is a bit trickier, but we can do it.

First, you might want to loop through the array to find whatever item it is you want to remove, unless you're removing by an index, doesn't really matter, but when you find that, take note of that items index.
Next, set an integer to loop through every item higher in the array than the item that you are removing, as an example, the item you want to remove is in the index 5 (which will be the variable i), then i2 will loop in the following manner:
[code]
For i2 = i To UBound(POBox)
If i2 < UBound(POBox) Then
POBox(i2) = POBox(i2 + 1)
End If
Next i2
If UBound(POBox) = 0 Then
POBox(0) = ""
Else
ReDim Preserve POBox(0 To UBound(POBox) - 1)
End If
[/code]
This is simply moving every item that is indexed above the item you want to remove, back one in the array, then shrinking the array size by one.

Hope this helped
April 27, 2004, 1:55 AM
FuzZ
[quote][quote]No, I really want to learn how to use them. Like how to make flag based database or what not. I don't wanna soure steal[/quote]



Step 1: Create a userdefined type for the users(with a var for username, access, flags, and whatever else you want.)
Step 2: Create an array of those types
Step 3: Manipulate the array as you see fit.

If you want any furthur detail, goto the library and getyourself a VB book and doit yourself. [/quote]

you know that's not gonna happen.

This is what I told someone that asked me last night

[code]
Public Type DataBase
UserName as string
Rank as string
SetBy as string
end type

Public DB() as DataBase
[/code]

I then referred them to msdn to look up arrays. I don't know if msdn has array information or not but he never said anything :p

If all else fails! http://www.google.com :)
April 27, 2004, 2:10 PM

Search