Click here to Skip to main content
15,889,889 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have to change file names serially, i change them with a stringbuilder array, put them in a "before" ListBox and in a "After" ListBox: while there are less than 19 items (height/itemHeight), everything works fine, if there isn't enough place, i get IndexOutOfRange exception

2.in the case i want to use try-catch, where do i have to put it?

I get the error in Program.cs at line Application.Run(new MainForm()); in Main() , do i have to catch exceptions here, or in the method Button_click, that add items to listBoxes?

i've seen that if you don't know what to do, you mustn't cacth exception, but i wanted to know a visual example (not the one in msdn.com, that uses console)

Thanks in advance

I get the error here:
C#
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new MainForm());//error here
    }

here i work with listboxes
      
    for (int i = 0; i < songsnum; i++)
    {
        songListBef.Items.Add(Path.GetFileName(full_fname_ex[i]));
        fname_noex_b[i] = new StringBuilder(Path.GetFileNameWithoutExtension(full_fname_ex[i]));
        fname_noex_b[i].Remove(0, toRemove_bef);
        fname_noex_b[i].Remove(fname_noex_b[i].Length - toRemove_aft, toRemove_aft);
    }

...
    
    for (int i = 0; i < songsnum; i++)
    {
        fname_noex_b[i].Append(Path.GetExtension(full_fname_ex[i]));
        songListAft.Items.Add(fname_noex_b[i].ToString());
    }
Posted
Updated 3-Sep-13 2:30am
v4
Comments
[no name] 3-Sep-13 8:29am    
"do i have to catch exceptions here", no. Catch the exceptions where they are likely to be thrown and handle them there.

"a visual example", there is no visual example or a console example.

The ListBix is unlikely to be the source of the exception as you are only adding items. My guess is that the Arrays (or Lists or other containers) full_name_ex and fname_noex_b only contain 19 items.

I would suggest you go to Debug/Exceptions... in Visual Studio and set the Checkboxes to get the exceptions the moment they are thrown. This should help in getting the debugger to the exact loaction.
 
Share this answer
 
Comments
maxpesa 4-Sep-13 6:59am    
for (int i = 0; i < songsnum ; i++)
{
fileListBef.Items.Add(Path.GetFileName(full_fname_ex[i]));
fname_noex_b[i] = new StringBuilder(Path.GetFileNameWithoutExtension(full_fname_ex[i]));
fname_noex_b[i].Remove(0, toRemove_bef); <---got exception here
fname_noex_b[i].Remove(fname_noex_b[i].Length - toRemove_aft,toRemove_aft);
}
Not sure if this is your issue but it probably is; it looks like you are trying to add one more item to the ListBox than there is in your item list.

Let's say songsum = 20 and you loop from 0 to 20, that's actually 21 items. If you only have 20 items then you will get an IndexOutOfRange error.

Try this in your loops, notice we are checking for one less than the songsnum value...
C#
for (int i = 0; i < songsnum - 1; i++)
{
    songListBef.Items.Add(Path.GetFileName(full_fname_ex[i]));
    fname_noex_b[i] = new StringBuilder(Path.GetFileNameWithoutExtension(full_fname_ex[i]));  
    fname_noex_b[i].Remove(0, toRemove_bef);
    fname_noex_b[i].Remove(fname_noex_b[i].Length - toRemove_aft, toRemove_aft);
}

...
C#
for (int i = 0; i < songsnum - 1; i++)
{
    fname_noex_b[i].Append(Path.GetExtension(full_fname_ex[i]));
    songListAft.Items.Add(fname_noex_b[i].ToString());
}
 
Share this answer
 
Comments
maxpesa 4-Sep-13 6:49am    
but if i have, for example, 16 items, i don't get indexout, so the loop should be fine.
i got the same problem by the way
Freak30 4-Sep-13 8:54am    
With your example code, you would only do 19 runs through the loop.

When you decrease songsnum and it works without exception, this means the arrays are not allocated with size songsnum but with a constant size.
idenizeni 4-Sep-13 12:59pm    
Thanks, you are correct. In my hast I was thinking equal to or less.
maxpesa 5-Sep-13 9:41am    
"decreasing", you intend "songsnum-1" or 16(or everything < 19) instead of 19?
Freak30 5-Sep-13 9:48am    
The comment about "decreasing" was targeted at the original poster who wrote, that if he only uses 16 items (possibly by setting songsnum to 16 instead of 17, though he doesn't say that explicitely), he doesn't get an Exception.

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