473,385 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Substring Error

22
Hey all,

This code:
Expand|Select|Wrap|Line Numbers
  1. AMessage = e.StackTrace.Substring(e.StackTrace.LastIndexOf("\\"), e.StackTrace.Length);
Produces this error:
Expand|Select|Wrap|Line Numbers
  1. Index and length must refer to a location within the string. Parameter name: length
I'm trying to retrieve the page name and line number of the given error (everything after the last slash in the StackTrace). I'm pretty sure my LastIndexOf and Length values are within range.

Any thoughts?
Dec 11 '08 #1
4 1841
nukefusion
221 Expert 100+
Substring returns a part of a string starting from the index you specify for the length you specify. If the length you specify is longer than what is left of the string you'll get this error. As your starting from somewhere in the middle of the string you can't read e.StackTrace.Length chars from the rest of the string - there aren't that many. :)

You need to change your code to read:

Expand|Select|Wrap|Line Numbers
  1. int index = e.StackTrace.LastIndexOf("\\");
  2. AMessage = e.StackTrace.Substring(index, e.StackTrace.Length - index); 
  3.  
Dec 11 '08 #2
Mugs321
22
Riiiight.... silly rabbit.. thx!
Dec 11 '08 #3
Plater
7,872 Expert 4TB
Although the error sounds like you're trying to start from index -1, because that search string does not exist in the string.
Be sure to check to make sure index != -1
Dec 11 '08 #4
nukefusion
221 Expert 100+
@Plater
Yes, that's a good suggestion. You may want to check that the index is valid before proceeding with the substring operation. In the above case the compiler has pointed out that the "length" parameter is the one at fault this time round, but you'd certainly want to employ Platers suggestion to make your code more robust.
Dec 11 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Don Freeman | last post by:
Seems like whatever value I use for the first int field (starting position) the substring procedure negates it and triggers a String index out of range error. I've tried all sorts of work...
11
by: Darren Anderson | last post by:
I have a function that I've tried using in an if then statement and I've found that no matter how much reworking I do with the code, the expected result is incorrect. the code: If Not...
2
by: Michele Fondry via .NET 247 | last post by:
hello. I have a webform form app in vb.Net that uses a SQL query. I am trying to use the substring function, but get the following error: Description: An unhandled exception occurred during the...
5
by: btober | last post by:
I can't seem to get right the regular expression for parsing data like these four sample rows (names and addresses changed to ficticious values) from a text-type column: Yolanda Harris, 38, of...
2
by: David Filion | last post by:
Hi, I have a question about substring(), when I run the following query: prepaid=# select substring('15148300', 0, 5); substring ----------- 1514 (1 row)
15
by: Duncan Allen | last post by:
Hi, Using C# I'm trying to use the substring method of a string variable but it just generates an "error: 'variable.Substring' does not exist " exception - how do I fix this ? code example: ...
1
by: rh1200la | last post by:
Hey all. I'm doing a very basic screen scrape of a page that displays a banner ad. It is basically an <Atag and an <IMGtag. I need to grab the url of each and store them. Right now I can...
6
by: kellygreer1 | last post by:
What is a good one line method for doing a "length safe" String.Substring? The VB classes offer up the old Left function so that string s = Microsoft.VisualBasic.Left("kelly",200) // s will =...
4
by: Jason | last post by:
I have a string that looks like this? ihelloworld_zzz_yyy In a single vb.net line, I want to remove the frist character and anything after and including the first "_" resulting in : ...
11
by: dyc | last post by:
how do i make use of substring method in order to extract the specified data from a a long string? I also need to do some checking b4 extracting the data, for instance: it only will extract the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.