0

When I am using StringReader.ReadLine to read text from array of strings, it reads it perfectly but it also clears my array,

using (StringReader reader = new StringReader(filesGroupList[x]))
       {
          while ((filesGroupList[x] = reader.ReadLine()) != null)
             {
                ...
             }
        }

Now, filesGroupList is empty. So if i will want to read data from this string again it will give me null reference exception, so the only way for me is to create a copy of this array before using ReadLine, but is there chance to avoid it? so when StringReaded finished reading the line, my line still stays inside array.

inside
  • 2,609
  • 7
  • 42
  • 68

3 Answers3

1

Given the code that you have written, here's an example of what I see it doing:

//going to set filesGroupList[x] to a string and then see what happens.
filesGroupList[x] = "First line of string.\nSecond line of string.\n";
//now we go into the using portion.
using (StringReader reader = new StringReader(filesGroupList[x]))
{
  //reader has access to the whole string.
  while ((filesGroupList[x] = reader.ReadLine()) != null)
  {
    //first time through, filesGroupList[x] is set to "First line of string."
    //second time through, filesGroupLIst[x] is set to "Second line of string."
    Console.WriteLine(filesGroupList[x]);
  }
  //third time through, ReadLine() returns null.
  //filesGroupList[x] is set to null.
  //Code breaks out of while loop.
  Console.WriteLine(filesGroupList[x]); //outputs an empty line.
}
//outside of using, filesGroupList[x] still null.
Console.WriteLine(filesGroupList[x]); //also outputs an empty line.

Now, given my other answer where I suggest using line, we'll keep everything the same except for the line portion.

//going to set filesGroupList[x] to a string and then see what happens.
filesGroupList[x] = "First line of string.\nSecond line of string.\n";
using (StringReader reader = new StringReader(filesGroupList[x]))
{
  //reader has access to the whole string.
  string line;
  while ((line = reader.ReadLine()) != null)
  {
    //first time through, line is set to "First line of string."
    //second time through, line is set to "Second line of string."
    Console.WriteLine(line);
  }
  //third time through, ReadLine() returns null.
  //line is set to null.
  //filesGroupList[x] is still set to "First line of string.\nSecond line of string.\n"
  Console.WriteLine(line); //outputs an empty line.
  Console.Write(filesGroupList[x]); //outputs whole string (on 2 lines).
}
Console.WriteLine(line); //won't compile. line doesn't exist.
Console.Write(filesGroupList[x]); //outputs whole string (on 2 lines).

So I don't think you want to read from filesGroupList[x] and then store that in filesGroupList[x]. If the string in filesGroupList[x] has no end of line characters, you're simply putting that string right back into it (and then putting null in your next time through the while loop). If the string in filesGroupList[x] does have end of line characters, then each time through the while loop, you are putting part of the string back into filesGroupList[x], which I don't think is your intention.

Joel Rondeau
  • 7,198
  • 2
  • 39
  • 52
0

Edit

Indeed after the using statement the string is empty, it is expected since:

StringReader.Dispose: Releases all resources used by the TextReader object. (Inherited from TextReader.)

MSDN Link here

The question is why you want to do that, it doesn't seems reasonable what you want to do. No without more context info.


Your code function perfectly, or you cannot explain yourself, or your problems is in something completely unrelated.

I just tried myself:

static void Main(string[] args)
{
    string[] filesGroupList = new string[1];

    int x = 0;

    filesGroupList[x] = "String is here!";

    using (StringReader reader = new StringReader(filesGroupList[x]))
    {
        while ((filesGroupList[x] = reader.ReadLine()) != null)
        {
            Console.WriteLine("the string is here, I just checked");
            Console.WriteLine(filesGroupList[x]);
        }
    }
    Console.ReadLine();
}

And the output:

the string is here, I just checked

String is here!

Community
  • 1
  • 1
RMalke
  • 3,857
  • 25
  • 42
-1

I think your mistake is right here:

while ((filesGroupList[x] = reader.ReadLine()) != null)

You are changing the value of filesGroupList[x] each time you read and the last time, setting it to null.

If instead, you did something like this:

string line;
while ((line = reader.ReadLine()) != null)
...

Then once you were outside of the using, you would find filesGroupList[x] unchanged.

Joel Rondeau
  • 7,198
  • 2
  • 39
  • 52
  • then i am not reading my filesGroupList but i am reading line, which is empty. – inside Mar 08 '13 at 21:01
  • if reader is build the same way `new StringReader(filesGroupList[x])` then line is set to each line from filesGroupList[x]. It will only be empty after all lines have been read from filesGroupList[x]. – Joel Rondeau Mar 08 '13 at 21:07