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
next

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.

Recommended Answers

All 6 Replies

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))
    Next
Next

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
    TextBox4.Replace(key,transform(key))
Next

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

commented: +1 for scalability +2

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

Great! If you're all set, mark the question as solved please.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.