1. This forum is ARCHIVED! Visit the new Cloud Sixteen forums, codename Eden, at https://eden.cloudsixteen.com. These forums will remain archived for a few months before being closed down, so try to encourage plugin developers to begin moving their content across to the new forums.
Dismiss Notice
Hi Guest, you need a Steam account to register and post on these forums. Login with Steam at the top of the forums to get started!
Dismiss Notice
Hi Guest, do you want to buy HL2RP or another Clockwork schema? Visit the Cloud Sixteen Store to get started!

[C#] foreach

Discussion in 'Programming' started by SomeSortOfDuck, Mar 2, 2013.

  1. Code:
    string[] links = File.ReadAllLines(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\saved.txt");
                    foreach (string line in links);
                    {
                        listView1.Items.Add(line);
                    }
    The error is: The name 'line' does not exist in the current context.
    ----------------------------------------------------------------
    But I just declared it in the foreach? I've Googled foreach and people do it like this and don't get any errors. Help meh D:

    EDIT: I fixed it by declaring it outside the foreach, but when I do that it doesn't read the lines.

    Code:
    string line = null;
    It doesn't add the lines to the listview though. The code is above the foreach. Google isn't helping much either :(
     
  2. TJjokerR

    TJjokerR Formerly known as TJjokerR

    Now you have to know I've never coded in C#.

    But I think that semi colon right after the foreach is wrong there, its stopping before getting to the block that loops. That is why it didn't work the first time, because the scope from the loop ends at that semi colon.

    So yeah remove that semi colon so the block is "attached" to it.
     
  3. Razor

    Razor Guest

  4. TOO LATE KUROCHI. HUUUUUEEE.

    That was it, TJ. You are magic man. Many thanks.
     
  5. Razor

    Razor Guest

    But.. But T_T
    I REQUEST SOME CREDIT BISH
     
  6. diablo1675

    diablo1675 Guest

    ~Gives 50 cent~

    That's your pay. Happy holidays!
     
  7. You are magic too, Kurochi. You help me in Lua and you helped me here too, even if late, better than nuthins.

    Now, let's lay this sweet prince of a thread to rest.
     

Previous Readers (Total: 0)