locked
For Each Loop RRS feed

  • Question

  • Guys

    Im having real problems getting my head around foreach loop

    I have the following

         var readLine = await FileIO.ReadLinesAsync(sampleFile);
    foreach (String i in readLine)
         {
             string[] Split = readLine[i].Split(new Char[] { ',' });
    this.textoutput.Text = Split[0];
         }

    readLine is a string loaded from a CSV file and has 1295 []

    I need to loop through each string in the array, split it, check the first part and print it in a text box

    I get two errors with the code

    1. The best overloaded method match for 'System.Collections.Generic.IList<string>.this[int]' has some invalid arguments

    2.    Argument 1: cannot convert from 'string' to 'int'

    both on the readLine[i] part of the split line

    any ideas where i'm going wrong

    Mark

    Monday, November 18, 2013 5:09 PM

Answers

  • In your foreach the "i" variable is a string, not an indexer into the array of strings. Instead of
    readLine[i].Split(new Char[] { ',' });

    do i.Split...

    i.Split(new Char[] { ',' });

    --Rob
    • Proposed as answer by Dave SmitsMVP Monday, November 18, 2013 9:29 PM
    • Marked as answer by wambaugh1 Monday, November 18, 2013 11:28 PM
    Monday, November 18, 2013 7:01 PM
    Moderator

All replies

  • In your foreach the "i" variable is a string, not an indexer into the array of strings. Instead of
    readLine[i].Split(new Char[] { ',' });

    do i.Split...

    i.Split(new Char[] { ',' });

    --Rob
    • Proposed as answer by Dave SmitsMVP Monday, November 18, 2013 9:29 PM
    • Marked as answer by wambaugh1 Monday, November 18, 2013 11:28 PM
    Monday, November 18, 2013 7:01 PM
    Moderator
  • Thanks Rob Spot on

    Mark

    Monday, November 18, 2013 11:28 PM