Click here to Skip to main content
15,909,503 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I am working on getting the last 4 digits of a VIN #. I thought it would be something simple but for some reason i am getting a outofrange exception error.

C#
/** Last 4 digits of VIN # **/
string wholeVIN = record.vehicle.vehicleidentificationnumber;
string lastFourOfVIN = wholeVIN.Substring(wholeVIN.Length - 4, wholeVIN.Length-1);
Posted
Comments
Sergey Alexandrovich Kryukov 19-Oct-15 16:44pm    
You really, really need to learn resolving such "problems" all by yourself...
—SA

If you have concerns with such simple tasks, always use the debugger. And just read the documentation on what you are using: https://msdn.microsoft.com/en-us/library/aka44szs%28v=vs.110%29.aspx[^].

As you can see, the second parameter is the the sub-string length, so it should be 4. (But never hard-code immediate constants like 4.)

—SA
 
Share this answer
 
Second parameter is length of substring !
Try
C#
/** Last 4 digits of VIN # **/
string wholeVIN = record.vehicle.vehicleidentificationnumber;
string lastFourOfVIN = wholeVIN.Substring(wholeVIN.Length - 4, 4);
 
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