How can I iterate through the alphabet in Visual B

2020-07-03 07:50发布

问题:

I need to generate a loop that would iterate through each capital letter of the alphabet using Visual Basic (2008). What's the cleanest way to do this?

回答1:

For Each c In "ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray()
...
Next


回答2:

The simplest way to do this would probably be:

For ascii = 65 To 90
    Debug.Print(Chr(ascii))
Next

I'm not really sure if it's the cleanest though. I haven't worked in vb.net much since I started with c#.



回答3:

I figured it out. Anyone have a better solution?

To loop through the alphabet:

For i As Int16 = Convert.ToInt16("A"c) To Convert.ToInt16("Z"c)
        Dim letter As Char = Convert.ToChar(i)
        'Do my loop work...
Next

Letter will first equal "A", then "B", etc.

Anyone have a better solution?



回答4:

Here's another way to enumerate letters:

For Each letter As Char In Enumerable.Range(Convert.ToInt16("A"c), 26) _
                                     .Select(Function(i) Convert.ToChar(i))
    Console.WriteLine(letter)
Next


回答5:

Try this one

Dim a As String
a = 65
Do
If a > 90 Then
Exit Do
Else
List1.AddItem Chr(a)
a = a + 1
End If
Loop


回答6:

Sub Sel_1_sur_2()
Dim i As Long
Dim a As ChartObject
Dim rng As Range
Set rng = Range(Chr(65) & ":" & Chr(65))
For ascii = 65 To 90 Step 2
    Debug.Print (Chr(ascii))
    Set rng = Union(rng, Range(Chr(ascii) & ":" & Chr(ascii)))
Next
rng.Select
End Sub


回答7:

Module Abecedario
    Sub main()
        Dim letra As String
        letra = 65

        Console.WriteLine("El abecedario")
        Console.WriteLine("-------------")

        Do
            If letra > 90 Then
                Exit Do
            Else
                Console.Write(Chr(letra) & ", ")
                letra = letra + 1
            End If
        Loop

        Console.ReadLine()

    End Sub
End Module


标签: vb.net