Results 1 to 5 of 5

Thread: [02/03] Initialize Array to Range

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2007
    Posts
    115

    [02/03] Initialize Array to Range

    Can I initialize array to a range in one step?

    i.e dim alphabetArray(26) as string = {"a".."z"}

    what would be correct syntax?

  2. #2
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: [02/03] Initialize Array to Range

    The correct syntax would be this:
    Code:
    Dim alphabetArray() As Char = {"a"c, "b"c, "c"c, "d"c}
    I don't have time to code all 26 letters out for you, but you get the idea. The c after each string is to indicate it is a char datatype, not a string, since if you wanted an alphabet array, its really just all chars, not strings.

    So you can just take my code above, and add the rest of the alphabet in and you are all set. Also note that when defining an array like this with values, you don't assign it a dimension (of 26). That is determined automatically by the runtime based on how many elements you assigned to it.

    Here is another way to do it:
    Code:
    Dim alphabetArray() As Char = "ABCDEFGHIJKLMNOPQRSTUVQXYZ".ToCharArray

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [02/03] Initialize Array to Range

    You could also do this:
    vb.net Code:
    1. Dim alphabet(26 - 1) As Char
    2. Dim base As Integer = Convert.ToInt32("a"c)
    3.  
    4. For index As 0 To alphabet.GetUpperBound(0) Step 1
    5.     alphabet(index) = Convert.ToChar(base + index)
    6. Next index
    although you may not choose to. Note also that arrays are zero-based, so your original code is creating an array with 27 elements.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    Addicted Member
    Join Date
    Aug 2006
    Posts
    211

    Re: [02/03] Initialize Array to Range

    Is there a way to include based on ranges?
    like:

    Range1 = "A-M"
    Range2 = "N-Z"

    I read A-M from a file and want to convert it in my variable to include A, B, C, D, E, F, etc from that range.

    I think maybe jm's function might be able to do it with some modification but perhaps there is another easy way or built in command? I'd like to be able to support wide ranges like "A-M,Q,P,T-Z"
    Last edited by DssTrainer; Jul 19th, 2007 at 03:07 PM.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [02/03] Initialize Array to Range

    There isn't really as far as I'm aware. Chars are not numerical types like they are in C, so you can't loop from one Char to another. You'd have to do as suggested and convert the first and last Chars to numbers, then loop between them and convert each intermediate value back to a Char.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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