Solved

Creting Arrays

Posted on 2003-12-03
9
212 Views
Last Modified: 2010-05-18
I know C++, and using it allows me to create arrays.

Is it possible to create arrays in VB and please give me a simple code so I can create an array with 10 string elements.

Thank you.
0
Comment
Question by:digi_mind
9 Comments
 
LVL 10

Accepted Solution

by:
ADSaunders earned 20 total points
ID: 9865615
Hi digi_mind,
Dim MyArray(10) As String
DIm i As Integer
MyArray(0) = "String0"
MyArray(1) = "String1"
MyArray(2) = "String2"
MyArray(3) = "String3"
MyArray(4) = "String4"
MyArray(5) = "String5"
MyArray(6) = "String6"
MyArray(7) = "String7"
MyArray(8) = "String8"
MyArray(9) = "String9"

(could be done in a for next loop)
for i = 0 to 9



Regards .. Alan
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 9865621
Hi digi_mind,

Dim aryMyArray(10) As String

You then have an array with ten elements (0 - 9) that you can refer to like this:

aryMyArray(0) = "Hello"
aryMyArray(7) = "Goodbye"

Tim Cottee MCSD, MCDBA, CPIM
Brainbench MVP for Visual Basic
http://www.brainbench.com
0
 
LVL 10

Expert Comment

by:ADSaunders
ID: 9865660
Sorry, submitted before I meant to!


(could be done in a for next loop)
for i = 0 to 9
    MyArray(i) = Some Data
Next.

or even
Dim MyArray
Myarray = Array("String0", "String1","String2","String3","String4","String5")
Dim i
For i = 0 To 5
msgbox Myarray(i)
Next

 .. Alan
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 1

Expert Comment

by:Moglor
ID: 9867242
you can also create dynamic arrays:

dim MyArray() as string

'then you can redim it later on

Redim MyArray(10)

For i = 0 to 10
    MyArray(i) = "Hello"
Next i

'then if you want to add a few more...

Redim Preserve MyArray(20)

For i = 11 to 20
    MyArray(i) = "Good Bye"
Next i

0
 

Expert Comment

by:icode4food
ID: 9869354
>>Dim aryMyArray(10) As String
>>You then have an array with ten elements (0 - 9) that you can refer to like this:

That is NOT correct. That gives you eleven elements (0-10).

As stated in the MSDN Library:
Dim Counters(14) As Integer
Dim Sums(20) As Double

The first declaration creates an array with 15 elements, with index numbers running from 0 to 14. The second creates an array with 21 elements, with index numbers running from 0 to 20. The default lower bound is 0.

0
 
LVL 29

Expert Comment

by:leonstryker
ID: 10212274
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Split ADSaunders, TimCottee
 
Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

leonstryker
EE Cleanup Volunteer
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 10269505
No split, mind you 20 points is hardly enough to split anyway so don't worry about it.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

808 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