Click here to Skip to main content
15,881,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The following code produces an error System.ArgumentOutOfRangeException: when it receives the input string: "PSTN0/19-4242619791(""Attendant"" system/CR)" (all quotes are part of the string)

Function Llamada(ByVal cadena As String) As String
        Dim resp As String = Replace(cadena, Chr(34), "")

        If resp.StartsWith("PSTN") Then 
            resp = cadena.Substring(cadena.IndexOf("-") + 1, cadena.Length - cadena.IndexOf("-"))


I cannot understand why I get such answer, I suppose that is because of the /, because inputs like: """Compralotodo"" 2800555"
works good. The error is produced when it executes replace, no matter if cadena.replace(Chr(34),"") is used or the function replace is called. Can somebody help me with this?

What I have tried:

I tried using the function replace or string.replace, both gave the same error. Why replace cannot handle such string?
Posted
Updated 7-Apr-21 1:07am

Because your Substring is wrong.
Assume your string is "a-cde".
The index of '-' is 1, the length is 5.
So your Substring command works out as
VB
cadena.Substring(1 + 1, 5 - 1)

So the section you are trying to extract starts at index 2, and if four characters long.
Index 2 is 'c', so your are trying to collect four characters from the three remaining.

It can't do that, so you get a "bad argument" exception.
 
Share this answer
 
Comments
diginet0 7-Apr-21 10:32am    
Thank You, you're rigth, I undertood the problem, but i couldn't delete the question in time. It was something with the debugger that shows me the wrong line, when I figure out that I resolved the issue changing the index length
First, break out those cadena.IndexOf("-") calls into their own variable. That will make it easier to debug.

Second, USE THE DEBUGGER!!! You said "you suppose ...". Well, "suppossing" is nothing more than guessing. The debugger is there to eliminate guessing. The debugger doesn't debug code for you. It's there to tell you what the code is actually seeing and doing. It's there to debug YOU and your understanding of the code. It's a tool used to learn.

USE IT.
 
Share this answer
 
v2
Comments
diginet0 7-Apr-21 10:23am    
Yes I understood the problem but I couldn't delete my question in time. Yes I'm using the debugger and that was my confusion because the debugger shows the DIM line as the line with the issue when the line with the issue is the last line. The debugger, I don't know why, is not showing the correct line under execution. I was executing this routine line by line and when it show the error it shows wrongly that the line under execution is the first.
Thank You to all to take time and answer my question
Dave Kreskowiak 7-Apr-21 12:06pm    
Go to the Build menu and pick "Clean Solution". Then go back to the Build menu and pick "Build Solution".

If the debugger is showing the incorrect line, it usually means the source was modified after the executable was built and the executable and source no longer match up.
Yes I understood the problem but I couldn't delete my question in time. Yes I'm using the debugger and that was my confusion because the debugger shows the DIM line as the line with the issue when the line with the issue is the last line. The debugger, I don't know why, is not showing the correct line under execution. I was executing this routine line by line and when it show the error it shows wrongly that the line under execution is the first.
Thank You to all to take time and answer my question
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900