I have been using this method for a while and today it ran aground when I asked it to retrieve the last 6 characters from a string of only 3.
ArgumentOutOfRangeException was the logical but unexpected conclusion. I have tried to modify the above code to make it fault tolerant, but the compiler does not like my style.
Any suggestions?
TIA
Robert
public static class StringExtensions
{
public static string Right(this string s, int numberCharacters){
try
{
return s.Substring(s.Length - numberCharacters);
}
catch (ArgumentOutOfRangeException)
{
return s (fail);
}
}
}
The simplest way to manage the possible ArgumentOutOfRange exception is to prevent it:
public static string Right(this string s, int numberCharacters){
return (s.Length > numberCharacters) ? s.Substring(s.Length - numberCharacters) : s;
}
That code anticipates the number being passed in to the method being greater than the length of the string and manages it in a if clause (using the conditional/ternary operator)
If you want to actually catch the exception rather than preventing it, your code could look like this:
public static string Right(this string s, int numberCharacters){
try{
s = s.Substring(s.Length - numberCharacters);
}
catch(ArgumentOutOfRangeException ex){
}
return s;
}
They both compile fine. Sounds like your copy-paste mojo has temporarily deserted you. Perhaps you have an extra closing brace where there shouldn't be one. Also, where are you adding them? You should be adding one or other to a static class in a file in
your App_Code folder:
public static class MyExtensions
{
public static string Right(this string s, int numberCharacters){
return (s.Length > numberCharacters) ? s.Substring(s.Length - numberCharacters) : s;
}
}
You will have to add using System; to the top of the file if you want to try the version that catches the exception.
public static string Right(this string s, int numberCharacters){
return (s.Length > numberCharacters) ? s.Substring(s.Length - numberCharacters) : s;
}
save
run:
Server Error in '/' Application.
Compilation Error
Description:
An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Line 1: public static string Right(this string s, int numberCharacters){ Line 2: return (s.Length > numberCharacters) ? s.Substring(s.Length - numberCharacters) : s;
Line 3: }
Source File: c:\Users\robert.bla\Documents\My Web Sites\testappcode\App_Code\RightNos.cs
Line: 1
Including the class declaration? It seems that way from the error message you are getting and the line number that the first line of the method appears on. All methods in C# must be declared in a class. And all extension methods must be declared in a static,
non-nested class. Look at the code in my previous reply. Notice the class declaration:
public static class MyExtensions
{
// add methods here
}
wavemaster
Participant
1279 Points
1127 Posts
Managing ArgumentOutOfRangeException
Dec 03, 2012 02:33 AM|LINK
I have been using this method for a while and today it ran aground when I asked it to retrieve the last 6 characters from a string of only 3.
ArgumentOutOfRangeException was the logical but unexpected conclusion. I have tried to modify the above code to make it fault tolerant, but the compiler does not like my style.
Any suggestions?
TIA
Robert
public static class StringExtensions { public static string Right(this string s, int numberCharacters){ try { return s.Substring(s.Length - numberCharacters); } catch (ArgumentOutOfRangeException) { return s (fail); } } }ayanmesut
Member
219 Points
85 Posts
Re: how to get the last X characters from a string
Dec 03, 2012 04:54 AM|LINK
maye you do not have enough amount of characters in your string.
Mikesdotnett...
All-Star
154887 Points
19862 Posts
Moderator
MVP
Re: Managing ArgumentOutOfRangeException
Dec 03, 2012 05:08 AM|LINK
The simplest way to manage the possible ArgumentOutOfRange exception is to prevent it:
public static string Right(this string s, int numberCharacters){ return (s.Length > numberCharacters) ? s.Substring(s.Length - numberCharacters) : s; }That code anticipates the number being passed in to the method being greater than the length of the string and manages it in a if clause (using the conditional/ternary operator)
If you want to actually catch the exception rather than preventing it, your code could look like this:
public static string Right(this string s, int numberCharacters){ try{ s = s.Substring(s.Length - numberCharacters); } catch(ArgumentOutOfRangeException ex){ } return s; }Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
wavemaster
Participant
1279 Points
1127 Posts
Re: how to get the last X characters from a string
Dec 03, 2012 06:15 AM|LINK
correct!
I am testing my code with a few what if scenarios.
Longer strings than what is provided for will be caught and acted on
Shorter strings are causing the exception which I am thinking the code should be ready for.
wavemaster
Participant
1279 Points
1127 Posts
Re: Managing ArgumentOutOfRangeException
Dec 03, 2012 06:37 AM|LINK
Both suggestions fail to compile:
Expected class, delegate, enum, interface, or struct.
Mikesdotnett...
All-Star
154887 Points
19862 Posts
Moderator
MVP
Re: Managing ArgumentOutOfRangeException
Dec 03, 2012 06:57 AM|LINK
They both compile fine. Sounds like your copy-paste mojo has temporarily deserted you. Perhaps you have an extra closing brace where there shouldn't be one. Also, where are you adding them? You should be adding one or other to a static class in a file in your App_Code folder:
public static class MyExtensions { public static string Right(this string s, int numberCharacters){ return (s.Length > numberCharacters) ? s.Substring(s.Length - numberCharacters) : s; } }You will have to add using System; to the top of the file if you want to try the version that catches the exception.
Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
wavemaster
Participant
1279 Points
1127 Posts
Re: Managing ArgumentOutOfRangeException
Dec 03, 2012 07:39 AM|LINK
New starter site;
Create folder App_Code
Create new file Class (C#)
file name Rightnos.cs
delete all the standard stuff
paste:
public static string Right(this string s, int numberCharacters){ return (s.Length > numberCharacters) ? s.Substring(s.Length - numberCharacters) : s; }save
run:
Server Error in '/' Application.
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: CS1518: Expected class, delegate, enum, interface, or struct
Source Error:
<div class="expandable">Show Detailed Compiler Output:</div>Source File: c:\Users\robert.bla\Documents\My Web Sites\testappcode\App_Code\RightNos.cs Line: 1
<div class="expandable">Show Complete Compilation Source:</div>Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.17929
Mikesdotnett...
All-Star
154887 Points
19862 Posts
Moderator
MVP
Re: Managing ArgumentOutOfRangeException
Dec 03, 2012 09:34 AM|LINK
Including the class declaration? It seems that way from the error message you are getting and the line number that the first line of the method appears on. All methods in C# must be declared in a class. And all extension methods must be declared in a static, non-nested class. Look at the code in my previous reply. Notice the class declaration:
public static class MyExtensions { // add methods here }Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
wavemaster
Participant
1279 Points
1127 Posts
Re: Managing ArgumentOutOfRangeException
Dec 03, 2012 10:24 AM|LINK
I see. My apologies. Not getting enough sleep.