0

I did the following programming. Running the application shows an error message: Index was out of range. Must be non-negative and less than the size of the collection.

EDIT:

public void SetShortcuts()
    {
        List<string> Verknüpfung = new List<string>();
        int i = 0;
        int j = 0;

        try
        {
            foreach (string Datei in Directory.GetFiles(PfadShortcuts, "*.txt"))
            {
                Verknüpfung.AddRange(File.ReadAllLines(Datei, Encoding.UTF8));

                Image ShortcutIcon = new Image();
                ShortcutIcon.Source = new BitmapImage(new Uri(@"Fugue Icons\document.png", UriKind.Relative));
                ShortcutIcon.Height = 16;
                ShortcutIcon.Width = 16;
                ShortcutIcon.Stretch = Stretch.None;

                MenuItem Shortcut = new MenuItem();
                Shortcut.Icon = ShortcutIcon;
                Shortcut.Header = Verknüpfung[0 + i];
                Shortcut.Padding = new Thickness(5);
                Shortcut.Click += delegate { Process.Start(Verknüpfung[0 + j]); };

                Shortcuts.Items.Add(Shortcut);
                i += 2;
                j++;
            }
        }
        catch
        {
            Fehlermeldung_Main_Shortcuts();
        }
    }

Could you please help me? Thanks in advance.

Kind regards.

sjantke
  • 605
  • 4
  • 9
  • 35
  • 1
    You're not checking anywhere that i < Verknüpfung.Count() – Shai Jan 19 '14 at 13:43
  • Thanks. But why do I need to do that and where? – sjantke Jan 19 '14 at 13:44
  • Don't know, didn't bother reading your code as you're doing it all wrong. – Shai Jan 19 '14 at 14:00
  • @Shai Telling somebody that they are going it all wrong is almost as helpful as posting a question that says "It's broken, Fix it". How can you say it is all wrong when you have not even read it all properly? If it is all wrong then why not offer advise so he can learn to fix it and learn to do it all properly? – Joe_DM Jan 20 '14 at 23:28

2 Answers2

0

If you look at the error message, it actually tells you exactly what is happening (you just need to be able to speak the language). "Index was out of range" means that you had N items, and you tried to take the (N + 1) item. In other words, you are trying to get something that doesn't exist, probably due to faulty logic in your program, but it also could be that you expect to have N+1 items, but don't.

Best way to catch this is to use the debugger to first find out which line you are getting the exception on. Placing a breakpoint on the first line in your ForEach (Verknüpfung.AddRange) will get you into debugging it.

To eliminate, you will need to (1) fix your input files OR (2) resolve your logic error so you are not trying to read more items than exist in the array.

theMayer
  • 15,456
  • 7
  • 58
  • 90
0

Look at lines:

Verknüpfung.AddRange(File.ReadAllLines(Datei, Encoding.UTF8));

and

Shortcut.Click += delegate { Process.Start(Verknüpfung[1 + i]); };

Verknüpfung[1 + i] is one higher than then number of items in the list.

i seems to be incrementing faster than the list is populated.

Try changing

Shortcut.Click += delegate { Process.Start(Verknüpfung[1 + i]); };

to

Shortcut.Click += delegate { Process.Start(Verknüpfung[0 + i]); };
Joe_DM
  • 985
  • 1
  • 5
  • 12
  • Verknüpfung.Count() is 14. This is the number of lines I would like to work with. Verknüpfung[1 + i] is necessary to start at line "2" and to get the content of 2 lines afterwards. How can I fix it? – sjantke Jan 19 '14 at 15:13
  • Unless I misunderstand Verknüpfung is a new and empty list. It has no line 2 to start on. Verknüpfung[1 + i] is the array number which starts at 0. – Joe_DM Jan 19 '14 at 15:21
  • Well, "Shortcut.Header = Verknüpfung[0 + i];" works. It sets the header to a created menu. What I'm trying to do is to set a click event to every second line. Line 0: Hello; Line 1: Run website; Line 2: World; Line 3: Run website; ... – sjantke Jan 19 '14 at 15:34
  • In the first loop i = 0 so when you do "Shortcut.Header = Verknüpfung[0 + i];" 0 + i is still 0 which would work. If you put a watch on Verknüpfung and inspect it when the program breaks you can check how many arrays are in the item. – Joe_DM Jan 19 '14 at 15:48
  • The exception tells me, that there are 14 elements in my list. This is the correct number of elements. – sjantke Jan 19 '14 at 15:58
  • Thanks, now it works BETTER. There is no error message anymore, but the output is creepy: The app is always running a wrong process (and it's always the same one) and it switches between name and function in the displayed menuitems... like this: name, name, function, function, function, name, name as header title. – sjantke Jan 19 '14 at 16:54
  • EDIT (SEE ABOVE): I added a new int called j for the process, saying "Shortcut.Click += delegate { Process.Start(Verknüpfung[0 + j]); };". Than I changed your plan: I left i += 2; and put j++; Now the header names are always correct - only the processes are not working: They always run the same process (8th position, list element 7)... – sjantke Jan 19 '14 at 17:12
  • My advice would be to use the debugger and follow the loop step by step to see where it goes wrong. To be honest I am not sure what the rest of the program is doing so I can't help. – Joe_DM Jan 20 '14 at 02:42
  • What happens if you make "Shortcut.Click += delegate { Process.Start(Verknüpfung[0 + i]); }; – Joe_DM Jan 20 '14 at 02:46
  • You get the error message of "Index was out of range. Must be non-negative and less than the size of the collection." - but thanks for your help so far. ;) – sjantke Jan 20 '14 at 12:06