Home > front end >  Correct way to show an output from for loop in a label
Correct way to show an output from for loop in a label

Time:10-31

hi I have a code that can transform the lower case letters of each element of a String array using for loop. the problem is only the last element is appearing on the output(label) but shows fine on the debug output

    Dim lst() = {"apple", "banana", "cherry"}

    For Each item As String In lst
        Dim array() As Char = item.ToCharArray
        array(0) = Char.ToUpper(array(0))

        Dim newS = New String(array)
        Dim value As String = String.Join("/", newS)
        TextBox1.Text = value
        Debug.Write(value)

        Output.Text = value
    Next
    Debug.WriteLine("")

this is the problem that occurs, it changes the label into an into the last element with the uppercase letter as it it is meant to be, but i want the output to be the same as the debug output which is

AppleBananaCherry

CodePudding user response:

You don't need to explicitly define a Char array. A String has a Char(index) property which is the Default property. We can use this directly on the String. MyString(index)

We assign the new String that is returned by the Replace method to an element of the array. index starts at 0, the default value of an Integer, and is incremented on each iteration of the For Each loop.

Finally, after the For Each, we assign the Join to the label.

Your code and where it went wrong.

Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
    Dim lst() = {"apple", "banana", "cherry"}

    For Each item As String In lst
        Dim array() As Char = item.ToCharArray
        array(0) = Char.ToUpper(array(0))

        Dim newS = New String(array)
        Dim value As String = String.Join("/", newS)
        TextBox1.Text = value 'Overwrites the last value you assigned to the Text property
        Debug.Write(value) 'Adds on to the last value it wrote in the debug window.

        Label1.Text = value 'Overwrites the last value you assigned to the Text property
    Next
    Debug.WriteLine("")
End Sub

Corrected code.

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    Dim lst = {"apple", "banana", "cherry"}
    Dim index As Integer
    For Each item As String In lst
        lst(index) = item.Replace(item(0), Char.ToUpper(item(0)))
        index  = 1
    Next
    Label1.Text = String.Join("", lst)
End Sub

This is WinForms but the code you care about shouldn't be any different, just the Event procedure format.

CodePudding user response:

You can even make it in a single line without an explicit loop.

Dim lst = {"apple", "banana", "cherry"}
Output.Text = String.Join("", lst.Select(Function(x) CultureInfo.CurrentCulture.TextInfo.ToTitleCase(x))))

While it seems very "smart" there are a couple of things to consider.

  • If the original list is very big (meaning thousands of strings) this is less efficient because the Linq Select and the subsequent Join will create a new list doubling the memory used.

  • If you want to change more than the first character, the ToTitleCase method cannot work

  • Related