0

I have written a function replacing text within, for example, an array of textboxes.

The function:

Protected Sub Replace(ByVal c1 As Char, ByVal c2 As Char, ByVal ParamArray fields() As TextBox)

    For Each field As TextBox In fields
        field.Text = field.Text.Replace(c1, c2)
    Next

End Sub

Called by these codes (both have the same output):

Replace(" ", String.Empty, _
            TextBox1, _
            TextBox2 _
            )


Replace(" ", "", _
            TextBox1, _
            TextBox2 _
            )

Input: foo bar

Output: foo

Exptected: foobar

Why does it replace all text after the space instead of just the characters I'm giving it?

7
  • 1
    You should use Option Strict On to avoid implicit conversions. To make a enclosed quotes char act like a char insted of string use this: " "c Commented May 2, 2014 at 11:44
  • This replaces a space by a space, instead of nothing. Commented May 2, 2014 at 11:56
  • 1
    Minor point: you're using " " as the find part of the replace, instead of c1. Commented May 2, 2014 at 11:57
  • What I mean is you should use String for both, and of course, use c1 inside the method, as @JonEgerton said. The replace method takes two strings or two chars, it doesn't allow mixing, so it implicitly convert one of them Commented May 2, 2014 at 11:57
  • @SysDragon option strict on doesn't allow a string.empty. JonEgerton oops, I will change it... code from debugging ;).. thanks Commented May 2, 2014 at 11:59

1 Answer 1

1

I'm not able to find a solution to replace a single CHAR by an empty CHAR, since an empty char doesn't seem to exist, like an empty string does. I changed the sub to take in strings instead of chars, this works fine...even when replacing only 1 character.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, that's the correct solution. A variable of type char always contains exactly one character.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.