4

The following VB code works correctly and does not flag up any errors.

strLine = strLine.Replace(strLine.LastIndexOf(","), "")

However the same C# code doesn't:

strLine = strLine.Replace(strLine.LastIndexOf(","), "");

This will not compile as it says

The best overloaded method for 'string.Replace(string,string)' has some invalid arguements.

How come this works in VB but not in C#? and how do I fix this?

I thought it might be similar to C# string.Replace doesn't work but it implies that that code will infact complile.

Likewise with other string.Replace questions: string.Replace (or other string modification) not working, it appears they will infact compile, whereas mine will not.

13
  • 1
    I'm curious - did you read the documentation on MSDN for the method you're trying to use? Commented May 2, 2013 at 18:02
  • 4
    The following VB code removes all the "," seperators - no, it removes all occurences of the textual representation of the position of the last comma in the string of data. E.g., all occurences of "42". Commented May 2, 2013 at 18:04
  • @J.Steen - I was looking back at some of my schoolwork (which is all in VB) and just tried a straight copy/paste/modify to C#. I'm reading about it just now. Commented May 2, 2013 at 18:05
  • @GSerg - I'll edit that comment. I wasn't sure how it worked, I just knew it worked. Commented May 2, 2013 at 18:05
  • 1
    @Ewan It compiled, that's entirely different from working. That VB is rather non-sensical and almost certainly isn't doing what you want it to do. VB code is much more likely to compile and just not work when given nonsensical code; C# just won't compile until you give it sensible code. Commented May 2, 2013 at 18:10

6 Answers 6

8

LastIndexOf returns an integer, not a string. Replace takes string, string as parameters, you're passing it int, string, hence the exception The best overloaded method for 'string.Replace(string,string)' has some invalid arguements.

If you're just looking to remove all , use this:

strLine = strLine.Replace(",", "");

Based on your code, you may be only wanting to replace the last instance of , so try this if that's what you want:

StringBuilder sb = new StringBuilder(strLine);
sb[strLine.LastIndexOf(",")] = "";
strLine = sb.ToString();
Sign up to request clarification or add additional context in comments.

4 Comments

Beat me to it. VB works because it will attempt to convert the number to a string for you. If you add Option Strict On to the top of the file then you should see it fail to compile with a similar error to the C# error.
This answer is correct. It's just VB trying to implicitly cast objects for you under the hood. C# is better about not letting you get away with that.
@tnw - Thanks! I added the option strict on in my VB and got bombarded with thousands of errors in my entire school code. Goes to show they don't teach you anything in school!
@Ewan See my edit -- If you wanted to only replace the last instance of , then I accounted for that
1

Reading the documentation, I'm amazed that the first example works at all. string.Replace should receive either a couple of chars or a couple of strings, not an integer and then a string. Pheraps the VB version is converting the integer to its char representation automatically?

2 Comments

The project could have option strict off, allowing late-binding to silently convert the integer to a string.
True. Hadn't thought of that. I'm still skeptic as to it working correctly, as the OP said. More like it's building correctly.
0

In c# the String.Replace's first parameter must be either char or string, but you are using a integer(string.LastIndexOf returns a integer). Why not just use:

strLine = strLine.Replace(',', '');

Comments

0

Try the following

string.Replace("," , "");

3 Comments

Might want to add some details why that will work, not just a code dump
I was on the toilet looking at stack on my tiny phone. Code dump was all I could offer.
That's fine, this is more a FYI: short answers sometimes get placed on a review queue as "low quality", that's where I found yours. So it's important to add details for the OP as well as to make sure your answer doesn't get marked low quality.
0

In C# LastIndexOf returns an int, but Replace expects a string or a char as the first argument. I don't know VB to explain why your code works, but I can explain that in C# you can't pass an integer where a string or a char is expected.

In C#, this will do what you want:

strLine = strLine.Replace(',', ''); 

Hope that helps.

Comments

0

you can use String.Remove

strLine = strLine.Remove(strLine.LastIndexOf(','), 1);

Comments

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.