Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

Quickly initialize Variant and String arrays
[ All Languages » VB »  Arrays]

Total Hit ( 3457)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


Visual Basic doesn't provide any way to declare an array and initialize its elements at the same time. In most cases you end up with setting individual elements one by one, as in:

Click here to copy the following block
Dim strArray(0 To 3) As String
strArray(0) = "Spring"
strArray(1) = "Summer"
strArray(2) = "Fall"
strArray(3) = "Winter"

Under VB4, VB5, and VB6 you can create an array of Variants on the fly, using the Array() function:

Click here to copy the following block
Dim varArray() As Variant
varArray() = Array("Spring", "Summer", "Fall", "Winter")

but there is no similar function to create arrays of data types other than Variant. If you're using VB6, however, you can create String arrays using the Split() function:

Click here to copy the following block
Dim varArray() As String
' arrays returned by Split are always zero-based
varArray() = Split("Spring;Summer;Fall;Winter", ";")

Under VB6 you can also take advantage of the capability for a Function to return an array, and build your own array initialization routines, such as the following one:

Click here to copy the following block
Function ArrayInt(ParamArray values() As Variant) As Integer()
  Dim i As Long
  ReDim res(0 To UBound(values)) As Integer
  For i = 0 To UBound(values)
    res(i) = values(i)
  Next
  ArrayInt = res()
End Function

You could also build a routine that tests the type of values passed to it, and returns an array of the correct type. In this case, the function should be declared to return a Variant.


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.