|
-
Jun 21st, 2007, 03:36 PM
#1
Thread Starter
Lively Member
[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?
-
Jun 21st, 2007, 04:05 PM
#2
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
-
Jun 21st, 2007, 08:10 PM
#3
Re: [02/03] Initialize Array to Range
You could also do this:
vb.net Code:
Dim alphabet(26 - 1) As Char
Dim base As Integer = Convert.ToInt32("a"c)
For index As 0 To alphabet.GetUpperBound(0) Step 1
alphabet(index) = Convert.ToChar(base + index)
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.
-
Jul 19th, 2007, 02:58 PM
#4
Addicted Member
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.
-
Jul 19th, 2007, 07:19 PM
#5
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|