Hello Developers, I'm here for your kind help,
I am making a program where these codes i wrote:

for i as integer = 0 to 5

                If TextBox4.Text.Contains("1") Then
                    TextBox4.Text = TextBox4.Text.Replace("1", "_n_")
                End If
                If TextBox4.Text.Contains("2") Then
                    TextBox4.Text = TextBox4.Text.Replace("2", "_o_")
                End If

                If TextBox4.Text.Contains("3") Then
                    TextBox4.Text = TextBox4.Text.Replace("3", "_p_")
                End If

                If TextBox4.Text.Contains("4") Then
                    TextBox4.Text = TextBox4.Text.Replace("4", "_q_")
                End If

                If TextBox4.Text.Contains("5") Then
                    TextBox4.Text = TextBox4.Text.Replace("5", "_r_")
                End If

Now the problem praised to me is i have over 100 if condition with this loop (i just show here 5),
so, i have to write if condition 100 times, i dont have an idea to get short this code.
can anybody help me to short this code with few lines? i dont want to type 100 times.
Please help me.

4 Years
Discussion Span
Last Post by Reverend Jim

Use a nested For loop.
please step through this code, I wrote it off the cuff without the IDE, and without testing, it may have undesired "features"

' this will get you started, but you'll need to figure out
' how you're going to handle the rest when you run out of
' straight number to letter substitutions
For i = 0 to 51                             ' only 26 lower and 26 upper
    For char = 65 to 122                    ' ascii values for A-Z, [ \ ] ^ _ `, a-z
        If ((char > 90) AND (char < 97))    ' skip the non-letter characters
            char = 97
        End If
        TextBox4.Text.Replace(i , Chr(char))

Edited by john.knapp: disclaimer


You could also use a dictionary as in

Dim transform As Dictionary(Of String, String)

and populate it where the key is the original char and the value is the replacement. Then you can do the replacement by

For Each key as String In transform.Keys

Caution - this will fail if any of the replacement strings contains any of the chars you are replacing.

Votes + Comments
+1 for scalability

Here's another method:

Dim Replacements() As String = {"_n_", "_o_", "_p_", "_q_", "_r_"}
Private Sub ReplaceText()
    For i = 0 To 4
        If TextBox1.Text.Contains(Str(i + 1)) Then
            TextBox1.Text = TextBox1.Text.Replace(Str(i + 1), Replacements(i))
        End If
End Sub

Just add any other values you want in 'replacements' and change the for loop to the number of elements-1.


thanks it helped me...
I Solved it...
Thanks again for all comments...


The test

If TextBox1.Text.Contains(Str(i + 1)) Then

is not necessary. If the first string is not present then the replace won't do anything.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.