Click here to Skip to main content
13,558,141 members
Rate this:
Please Sign up or sign in to vote.
See more:
i've got an index out of range exeption on this one

If My.Settings.SavedLinks IsNot Nothing Then
            For Each Sl As String In My.Settings.SavedLinks
                Dim LinkInfo As String = Sl.Split(",").ToString
                Dim NewLink As New ToolStripButton(LinkInfo(0))
                NewLink.Tag = LinkInfo(1)'it gives the error here
                AddHandler NewLink.Click, AddressOf GoToLink
                Dim sep As New ToolStripSeparator
        End If

i dont know what im doing wrong but when it loads it give the error

its for a favorite bar in my webbrowser
Posted 27-Jan-13 3:38am
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Use the debugger:
1) set to stop on throwing exception (Debug --> Exceptions --> .Net Exceptions --> throwing [x])
2) run the program in debug mode
3) when the debugger stops where the execption is thrown, check the message and inspect the individual variables
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

One of your strings does not contain a ',' character. As a result, there is only one entry in the LinkInfo array, and when you try to access the second one it throws an exception.
Put a breakpoint on the line that throws the error, and look at the value in S1.
Bart de Lange 27-Jan-13 10:13am
no it does not contain any string in my.settings.savedlinks but it gives the error
Bart de Lange 27-Jan-13 10:17am
and how do i put a breakpoint to a line??
OriginalGriff 27-Jan-13 10:35am
Easiest way is to put the cursor on the line, then go to the menu: "Debug...Toggle Breakpoint" (it will also show you a short cut key, probably F9). If you click it, it will put a red dot to the left of the line. to show where the breakpoint is. In future, you can just click where the red dot appears to add a breakpoint to that line (now that you know where it is!)

If your settings.SaveLinks contains no strings, then it wouldn't get to the error point, so it contains at least one string - which may be the empty string, just as a "place holder".
If you add a test as the first thing you do in the loop:
If Not String.IsNullOrWhiteSpace(S1) Then
End If
Then that would help, as would checking the size of the array before you use it at all:
Dim LinkInfo As String = Sl.Split(",").ToString
If LinkInfo.Length >= 2 Then
End If
Bart de Lange 27-Jan-13 10:42am
thanks it works
OriginalGriff 27-Jan-13 11:11am
You're welcome!

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.180515.1 | Last Updated 27 Jan 2013
Copyright © CodeProject, 1999-2018
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