Results 1 to 7 of 7

Thread: Fill an array in one operation

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2000
    Location
    London, UK
    Posts
    145
    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
    Wilhelm Tunemyr,
    Swede in London

    [email protected]

    "Dort, wo man Bücher verbrennt, verbrennt man am Ende auch Menschen"
    Heinrich Heine (1797-1856)

    Pravda vítezi!
    (Truth prevails!)

  2. #2
    Frenzied Member
    Join Date
    Mar 2000
    Posts
    1,089
    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.

  3. #3
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177
    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

  4. #4
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657
    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".
    "It's cold gin time again ..."

    Check out my website here.

  5. #5
    Guest
    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

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Feb 2000
    Location
    London, UK
    Posts
    145
    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
    Wilhelm Tunemyr,
    Swede in London

    [email protected]

    "Dort, wo man Bücher verbrennt, verbrennt man am Ende auch Menschen"
    Heinrich Heine (1797-1856)

    Pravda vítezi!
    (Truth prevails!)

  7. #7
    Guest
    You must declare it as a variant at first, but once you split it, the datatype will then be converted into a String array.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width