Can I initialize array to a range in one step?
i.e dim alphabetArray(26) as string = {"a".."z"}
what would be correct syntax?
Printable View
Can I initialize array to a range in one step?
i.e dim alphabetArray(26) as string = {"a".."z"}
what would be correct syntax?
The correct syntax would be this:
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.Code:Dim alphabetArray() As Char = {"a"c, "b"c, "c"c, "d"c}
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
You could also do this:although you may not choose to. Note also that arrays are zero-based, so your original code is creating an array with 27 elements.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
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"
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.