Click here to Skip to main content
13,293,950 members (59,876 online)
Rate this:
Please Sign up or sign in to vote.
See more:
That's the error I get. I understand this error means that one of the numbers is out of range--BUT ITS NOT! Here's my first code snippet (trying to get the last four characters out of 'Label1', using a substring. I call this substring "filetype"):

filetype = Label1.Text.Substring(Label1.Text.Length - 4,_ Label1.Text.Length)

This error is pretty much saying that the length of Label1 exceeds the length of Label1. Makes no sense. I was afraid that Label1 actually has no value when I call the substring, so I thought what I should use to get the text should be what Label1 uses to get its text. Label1 uses Form1.ListBox2.SelectedItem. So, my second code snippet:

filetype = Form1.ListBox2.SelectedItem.ToString.Substring(Form1.ListBox2.SelectedItem.ToString.Length - 4, Form1.ListBox2.SelectedItem.ToString.Length)

And I get the same error. So then I look at the details, and it says that Label1's text is 51 characters. So, my third code snippet, just to test it:

filetype = Label1.Text.Substring(47, 51)

AND BELIEVE IT OR NOT, I GET THE SAME ERROR! It clearly says the length is 51. I'm just so stumped.

So thanks for reading my whole question.

Posted 22-Dec-12 2:29am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1


Your code snippet:
filetype = Label1.Text.Substring(47, 51);

The startIndex is 47, and the length is 51. 51 isn't where the substring needs to end. 51 is the length of the substring you want to take.
Read more here:[^]
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

OK, unless I'm missing something obvious then it's simply that you've misinterpreted the second parameter.
it's the length of the string you want to extract, not the length of what you started with.
"TheBilly102030".Substring(3, 5) = "Billy"

"TheBilly102030".Substring(14 - 4, 14) = Error
Since you are asking it to grab 14 characters from a 14 character string starting at position 10, so it goes looking for characters between 10 and 24, most of which don't exist...

If what you want is the last three characters, or the last four, ot everything after the last "." then:

Dim filename as string = "TheBilly102030.doc"
'Change 3 to 4 for a 4 character file extension
Dim filetype as string = filename.Substring(filename.length - 3, 3) = "doc"

The variable version is done simply by grabbing the position of the last "." and substituting that for the literal number used above.


Rate this: bad
Please Sign up or sign in to vote.

Solution 3

Thanks SO MUCH! It works perfectly now!!!

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.171207.1 | Last Updated 22 Dec 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100