-1

I want to do something like the following:

int loopCount = 0;
while (loopCount != 18)
{
    StreamWriter DBSplit + loopCount = 
    new StreamWriter(@"L:\BananaDB\DBFILE" + loopCount + ".txt");
    loopCount++;
}

What is the best way to do this? At the moment the problem is "DBSplit + loopCount". While yes it is because I'm trying to add an int to the StreamWriter variable, I cannot use string either. Is there any solution?

Jaap
  • 81,064
  • 34
  • 182
  • 193
Mitchell
  • 169
  • 1
  • 14
  • As ever, if you want multiple variables indexed by number, use an array or a list. (If you want multiple variables indexed by name, use a `Dictionary` instead.) There's nothing specific about `StreamWriter` here - other than the fact that you'll need to be careful to dispose of everything. – Jon Skeet Jul 11 '14 at 07:20
  • IMO, This sounds like a design flaw (saving what I guess is the same data in 18 different places?) and a liability to make sure that the streams are closed correctly – Sayse Jul 11 '14 at 07:24
  • No, it's not a design flaw. I need to split a large file in to 18 smaller ones. – Mitchell Jul 11 '14 at 07:26
  • I still dont see why all 18 streams need to be open at once. – Sayse Jul 11 '14 at 07:31
  • My program is reading line by line from a very large file, depending on what information is on that line, it will be written to a particular file. That is why. – Mitchell Jul 11 '14 at 07:34
  • Can someone explain to me why my question deserves -2? What is wrong with it? – Mitchell Jul 11 '14 at 07:35
  • @Mitchell It's not a bad question, bumped you up one. – Hjalmar Z Jul 11 '14 at 07:47

1 Answers1

3

Use an array ? Also you can replace while with a for loop:

var writers = new StreamWriter[18];

for(int i =0; i<18; i++)
    writers[i] = new StreamWriter(@"L:\BananaDB\DBFILE" + i + ".txt");
Selman Genç
  • 100,147
  • 13
  • 119
  • 184