Mark Meuer
Mark Meuer

Reputation: 7523

How can I iterate through the alphabet in Visual Basic?

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?

Upvotes: 15

Views: 59540

Answers (8)

Hopsy24
Hopsy24

Reputation: 11

Maybe there is an easier way, but I was able to put this together in a minute.

 Dim alpha As String
 alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
 For x = 1 To 26
      c = Mid(alpha, x, 1)
      ...
 Next

Upvotes: 1

Martin Deveault
Martin Deveault

Reputation: 1

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

Upvotes: 0

José Matos
José Matos

Reputation: 1

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

Upvotes: -1

janmark
janmark

Reputation: 9

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

Upvotes: 0

Meta-Knight
Meta-Knight

Reputation: 17845

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

Upvotes: 1

asleepysamurai
asleepysamurai

Reputation: 1372

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

Upvotes: 18

manji
manji

Reputation: 47978

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

Upvotes: 33

Mark Meuer
Mark Meuer

Reputation: 7523

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?

Upvotes: 12

Related Questions