Question Integer value changing when trying to compare cell data by using 'IndexOf' feature

cypress1976

Member
Joined
Aug 18, 2009
Messages
7
Programming Experience
3-5
I have a program that is reading an Excel (xlsx) file and I am trying to take the data found in one of the cells and split it based upon the '@' symbol inside that cell.

Here's an example:
value in cell is "XXX @ YYY"
I declare 3 strings (strOriginal, strA, strH)
strOriginal is the value from the cell ("XXX @ YYY")
then my code is supposed to split up the values in the Original string based upon that '@' symbol.
So when my code is done, strA should = 'XXX' and strH should = 'YYY'.

Here is my code

VB.NET:
            strOriginal = dtbExcelData.Rows(0).Item(0).ToString
            y = strOriginal.IndexOf("@")
            strA = strOriginal.Substring(0, y - 1)
            strH = strOriginal.Substring(y + 1, strOriginal.Length - 1)

While debugging my code, I know that the strOriginal is getting the proper "XXX @ YYY" value and I also know that y is getting the index position of the '@' symbol - in this instance, it would be '4'. And strA gets the proper modified value of "XXX".

The problem occurs in the last line of the above code
VB.NET:
strH = strOriginal.Substring(y + 1, strOriginal.Length - 1)

For some unknown reason, the y integer (in this case, it is '4') changes to a random number (it has changed to 44, 61, 124) and causes the error "Error with Excel file: Index and length must refer to a location within the string. Parameter name: length" to occur.

I understand why the error occurs (my y integer is not staying at 4)...I just don't understand why that y integer is changing numbers when it should remain as the indexof the '@' symbol. I'm looking for any suggestions on how to pull out the data after the space after the '@' symbol (the 'YYY' portion of 'XXX @ YYY'). Also sometimes the 'YYY' is only 'YY' as well.

Hopefully I have provided enough information. Thanks.
 
The problem occurs in the last line of the above code
Code:
strH = strOriginal.Substring(y + 1, strOriginal.Length - 1)
You're using Substring(startIndex, length), while for unknown reasons you believe it is (startIndex, endIndex), logically that code would go beyond end of string. You can calculate length properly, but you can also use the Substring(startIndex) method that returns the string from given index to end of string.
 
Thank you for the replies. Let me put a correction in on my one line of code where the problem is occurring.

VB.NET:
strH = strOriginal.Substring(y + 1, strOriginal.Length)

That is my actual code that I am using. Y being the placement of the '@' symbol in the string and then the other part obviously being the total string length. What's occurring when I executing is that the value for integer y (which should be '4') is somehow changing numbers and I don't understand why.

My whole code for this section looks like this:

VB.NET:
strOriginal= dtbExcelData.Rows(0).Item(0).ToString
y = strOriginal.IndexOf("@")
strA = strOriginal.Substring(0, y - 1)
MsgBox(strA)

y = y + 1
strH = strOriginal.Substring(y, strOriginal.Length)
MsgBox(strH)

and the y integer shows the proper value for the first part
VB.NET:
strOriginal= dtbExcelData.Rows(0).Item(0).ToString
y = strOriginal.IndexOf("@")
strA = strOriginal.Substring(0, y - 1)
MsgBox(strA)
but when the code continues onto the second part, that y integer (which was '4' during the execution of the top section, suddenly changes to some random number and then errors out on that next section. That is where the confusion lies because, to me, that y integer should not be changing numbers. Thanks again.
 
Y is not changing, it can't be.
strOriginal.Substring(y + 1, strOriginal.Length)
You can't see that if y is 0 or larger here then that call will throw exception??
other part obviously being the total string length
That is the problem, if your string is length 10 and you start at 4th char you can at max grab the next 6 chars (length=6), because there are only 10 to take from. Your code is attempting to grab more chars than is available in the string. The length parameter specifies how many chars Substring should return from given start index, and not total length of string.
As I also explained, if you're going to get remaining chars from startIndex, just use the Substring(index) method and not worry about calculating the length of chars to grab.
 
Back
Top