-
Hello!
Is it possible to fill an array with one line och code?
For instance, if I want an array of strings with the days of the week, I could do it this way:
Dim week(7) as String
week(1) = "Monday"
week(2) = "Tuesday"
week(3) = "Wednesday"
...
week(7) = "Sunday"
But there must be a better way.
Some sort of
Dim week(7) as String
week = "Monday";"Tuesday";"Wednesday"..."Sunday"
or something.
It's possible in C++, allthough I've forgotten how to do it right now, so it must be a way.
Thanks for your help!
Pentax
-
I don't think so, it's not difficult to code though, even if there was it wouldn't be any faster to use the in built function than your own.
-
You can use the Split() function in VB6:
Code:
Dim vWeek As Variant
vWeek = Split("Monday,Tuesday,Wednesday,Thursday,Friday,Saturday,Sunday", ",")
Or the Format() Function:
Code:
Dim iDay As Integer
For iDay = 1 To 7
Debug.Print Format(CDate("01/" & iDay + 2 & "/2000"), "DDDD")
Next
-
You can use the Array function (the data type of the array must be variant, and it will start at index 0):
Code:
Dim week as Variant
week = Array("Sunday", "Monday", "Tuesday", .... "Saturday")
week(0) will refer to "Sunday", week(6) will refer to "Saturday".
-
If you want your Array to start at 1, use the Option Base 1 statement.
Code:
Option Base 1
Private Sub Command1_Click()
MyAray = Array("Monday", "Tuesday")
Print MyArray(1)
End Sub
-
OK, thanks!
It's not so much for the speed as for my lazyness.
It takes less effort to do it this way.
But can it really not be done with an array of strings?
Pentax
-
You must declare it as a variant at first, but once you split it, the datatype will then be converted into a String array.