Solved

VB Arrays

Posted on 2004-08-22
9
1,403 Views
Last Modified: 2012-06-27
Hi,

Just trying to figure out how to do this with a VB array. I want an array that does the equivalent of this PHP code:

$db_array[1][0] = username
$db_array[1][1] = userid
$db_array[1][2] = email      

Where the first number in the [] would go up from 1 to however number of users I have. The second number in the [] would mean the username [0], userid [1] or email [2] of that first number.

Make sense?

Thanks a lot :-), and let me know if you need clarification. I know I've been pretty vague, but I'm unsure as how I should explain it ;-).
0
Comment
Question by:Jimmy_A
9 Comments
 
LVL 22

Assisted Solution

by:DarkoLord
DarkoLord earned 34 total points
ID: 11863183
Hi...

First you use:
    Dim db_array() As String

And then redimension it using:
    ReDim Preserver db_array(nuber_of_users, 2) As String


Darko
0
 
LVL 4

Assisted Solution

by:hamood
hamood earned 33 total points
ID: 11863271
Basically, you can create either static or dynamic arrays. Static arrays must include a fixed number of items, and this number must be known at compile time so that the compiler can set aside the necessary amount of memory. You create a static array using a Dim statement with a constant argument:

' This is a static array.
Dim Names(100) As String

Visual Basic starts indexing the array with 0. Therefore, the preceding array actually holds 101 items.

Most programs don't use static arrays because programmers rarely know at compile time how many items you need and also because static arrays can't be resized during execution. Both these issues are solved by dynamic arrays. You declare and create dynamic arrays in two distinct steps. In general, you declare the array to account for its visibility (for example, at the beginning of a module if you want to make it visible by all the procedures of the module) using a Dim command with an empty pair of brackets. Then you create the array when you actually need it, using a ReDim statement:

' An array defined in a BAS module (with Private scope)
Dim Customers() As String
...
Sub Main()
    ' Here you create the array.
    ReDim Customer(1000) As String
End Sub

If you're creating an array that's local to a procedure, you can do everything with a single ReDim statement:

Sub PrintReport()
    ' This array is visible only to the procedure.
    ReDim Customers(1000) As String
    ' ...
End Sub

If you don't specify the lower index of an array, Visual Basic assumes it to be 0, unless an Option Base 1 statement is placed at the beginning of the module. My suggestion is this: Never use an Option Base statement because it makes code reuse more difficult. (You can't cut and paste routines without worrying about the current Option Base.) If you want to explicitly use a lower index different from 0, use this syntax instead:

ReDim Customers(1 To 1000) As String

Dynamic arrays can be re-created at will, each time with a different number of items. When you re-create a dynamic array, its contents are reset to 0 (or to an empty string) and you lose the data it contains. If you want to resize an array without losing its contents, use the ReDim Preserve command:

ReDim Preserve Customers(2000) As String

When you're resizing an array, you can't change the number of its dimensions nor the type of the values it contains. Moreover, when you're using ReDim Preserve on a multidimensional array, you can resize only its last dimension:

ReDim Cells(1 To 100, 10) As Integer
...
ReDim Preserve Cells(1 To 100, 20) As Integer    ' This works.
ReDim Preserve Cells(1 To 200, 20) As Integer    ' This doesn't.

Finally, you can destroy an array using the Erase statement. If the array is dynamic, Visual Basic releases the memory allocated for its elements (and you can't read or write them any longer); if the array is static, its elements are set to 0 or to empty strings.

You can use the LBound and UBound functions to retrieve the lower and upper indices. If the array has two or more dimensions, you need to pass a second argument to these functions to specify the dimension you need:

Print LBound(Cells, 1)   ' Displays 1, lower index of 1st dimension
Print LBound(Cells)      ' Same as above
Print UBound(Cells, 2)   ' Displays 20, upper index of 2nd dimension
' Evaluate total number of elements.
NumEls = (UBound(Cells) _ LBound(Cells) + 1) * _
    (UBound(Cells, 2) _ LBound(Cells, 2) + 1)

hamood

0
 
LVL 22

Expert Comment

by:DarkoLord
ID: 11863277
Well in his case (dynamic first dimension) is best to use dynamic multi-dimensional array as I wrote in my post above ;)

Darko
0
 
LVL 18

Assisted Solution

by:JR2003
JR2003 earned 33 total points
ID: 11863518
Jimmy_A,

I think this is what you are looking for:

Dim db_array() as String
Dim NumberOfPeople as Long

NumberOfPeople = 20 'Or however many there are.

Redim db_array(1 to NumberOfPeople, 0 to 2)

db_array(1, 0) = username
db_array(1, 1) = userid
db_array(1, 2) = email

JR2003
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 7

Accepted Solution

by:
Enlade earned 50 total points
ID: 11864295

It is better to use a structure instead of a 2d array.  Maybe try this instead.  Put this type in a module called modTypes or something.

Public Type UserType
  Username As String
  UserID as String
  Email as String
End type

Public Type GroupType
  Person() As UserType
  nPeople As Long
End Type


Then you probably have some global variable that you will put the people into.  Maybe add them to another module called modGlobs or something.  Like this:

Public People As GroupType


Then you fill the People type...I'll just hard code a person or two for you like this:

People.nPeople = 2
Redim People.Person(1 To People.nPeople) As UserType

People.Person(1).Username = "Jim"
People.Person(1).UserID = "JJ"
People.Person(1).Email = "jj@aol.com"

People.Person(2).Username = "Nancy"
People.Person(2).UserID = "Nan"
People.Person(2).Email = "nan@aol.com"

0
 
LVL 7

Expert Comment

by:Enlade
ID: 11864307

You might want to redim from 0 instead of 1.  That depends on what you are doing.  You know something like this:

People.nPeople = 2
Redim People.Person(1 To (People.nPeople-1)) As UserType

People.Person(0).Username = "Jim"
People.Person(0).UserID = "JJ"
People.Person(0).Email = "jj@aol.com"

People.Person(1).Username = "Nancy"
People.Person(1).UserID = "Nan"
People.Person(1).Email = "nan@aol.com"

It really is no different, but some people like to start from 0 instead of 1 (or whatever).
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11864319

I recommend using types because it makes it easier for you to handle the data as you start to pass it around your program.  Like if you want to deal with one person then you just pass one variable of type UserType.  Instead of having to pass three variables for each field that you associate with the person.  There are a lot of other reasons to be using types instead of splitting your data up into multiple arrays.  Anyway, its just a suggesting.
0
 
LVL 7

Expert Comment

by:Enlade
ID: 11864339

Opps....I didn't redim that previous message from 0.  It still redim'ing from 1.  Though I did change the upper bounds.  The line:

Redim People.Person(1 To (People.nPeople-1)) As UserType

Should have read:

Redim People.Person(0 To (People.nPeople-1)) As UserType

If you wanted to start at 0 instead of 1.  There are probably other typos since I didn't test anything.  Still, you get the jist of what I am suggesting.
0
 

Author Comment

by:Jimmy_A
ID: 11865595
Whoa, this is what I call service :-). I wake up, and already have this many answers to my question.

I've ended up using the types Enlade detailed (which work perfectly by the way), so have given you the 50 points. I've learnt stuff some all of the other methods, so I've increased the points and split the extra points among the rest of you.

Thanks again! This has been very helpful.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now