• Welcome to Valhalla Legends Archive.
 

Array tutorial

Started by Networks, April 25, 2004, 01:20 PM

Previous topic - Next topic

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.

Grok

Quote from: Networks on April 25, 2004, 01:20 PM
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.

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


Networks

Now can you help impliment this into something confusing and advanced so I become an expert. Teach ME!

Grok

Is this a joke to see how much time of someone's you can waste today?  heh.

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 ;)

The-FooL

Quote from: Networks on April 25, 2004, 07:27 PM
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 ;)

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.

Networks

I have one but its explains a lot more about controls rather than actual code.

Eric

#7
Quote from: Networks on April 26, 2004, 01:48 PM
I have one but its explains a lot more about controls rather than actual code.

... All the code you need:

Quote from: Grok on April 25, 2004, 01:57 PM
Dim POBox(9) As Long

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:
Dim POBox(9) As Long
You would do:
Dim POBox() As Long
Before the arrays is initialized (on Form_Load, or perhaps before anything is done concerning the array) do:
ReDim POBox(0)
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:
ReDim Preserve POBox(0 to UBound(POBox) + 1)
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:

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

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

FuzZ

Quote
QuoteNo, 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



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.

you know that's not gonna happen.

This is what I told someone that asked me last night


Public Type DataBase
    UserName as string
    Rank as string
    SetBy as string
end type

Public DB() as DataBase


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 :)