tags:

views:

170

answers:

3

This is driving me crazy. How can I assign a set of text values to an array? Nothing I try is working!

Months = Array("Jan", "Feb",... etc does not work!

+1  A: 

I'm pretty sure you can only do it like this:

 dim months(2) as string

 months(0) = "Jan"
 months(1) = "Feb"
 months(2) = "Mar"
Mark Biek
How archaic. Never mind. Thanks anyway!
Chris Gunner
It's pretty annoying. I try to use Collections whenever possible
Mark Biek
+8  A: 

Here's something about VB: http://www.devx.com/vb2themax/Tip/18322

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:

  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:

  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:

  Dim varArray() As String 
  ' arrays returned by Split are always zero-based 
  varArray() = Split("Spring;Summer;Fall;Winter", ";")
Stephen Pape
That's a good trick. I didn't know about that one.
Mark Biek
Split works in VBA and VBScript.
Remou
+1  A: 

If you're talking about vbscript then this works:

months = Array("may","june","july")

If it's vb.net then:

dim months() as string = {"may","june","july"}
Seth Reno