I have the following situation:

- I need to iterate through many streamed, comma-delimited, input files
(with different numbers of comma de-limited fields and internal
structures); re-format them (selecting common fields from the different
files); and output them into a common file.
- The input files all have similar characteristics - I need to open them,
iterate through each line, determine how many commas there are per line,
and close them. It is the detailed formatting for each file that is
different.
- To achieve this I can define a parent class (FileMgr), and inherit the
majority of the functionality in file-specific sub-classes (FileMgrType1,
FileMgrType2,...). I then implement a file-type specific Format method in
the child classes.

So far, so good.

>From the client side, I would like to iterate in a loop, processing a new
file on each re-pass. At the start of iteration I would like to open the
new input file, and create a file-specific object that can format and hold
the variables for that type of file - using as few fileType specific lines
of code as possible. I include a (contrived) sample of the sort of thing
that I would like to do.

I (unsuprisingly) get compilation errors - error CS0128, local variable
already defined in this scope - because I am trying to 're-use' the same
variable name for different objects in my Main.

I could cut and paste the same code (with slightly different variable
names) - but if I need to open 50 different files, then this becomes
cumbersome.

Is there anyway of achieving what I want to do?

Regards

-Steve Simpson


using System;
using System.IO;
using System.Text;

namespace PolyClient
{
 /// <summary>
 /// Summary description for Class1.
 /// </summary>
 ///

 // A class to format & hold an input record as an intermediate,
formatted object
 class FileMgr
 {
  public int numberFieldsInInput;

  public string [] fields;

  // a whole load of common attributes, and associated public
accessors to hold the data elements
  // public string TodaysDate;
  // public int RecordId;
  // etc. etc.

  virtual public void Format ( string foo)
  {
   // extract the field values from the string, and
put into the class members.
  }
 }

 // A class to do formatting for Type1s
 class FileMgrType1 : FileMgr
 {
  public void setnumberFieldsInInput()
  {
   numberFieldsInInput = 10 ;
  }

  override public void Format (string foo)
  {
   fields = foo.Split(new Char[] {','});
   // do a load of formatting here
  }

  // A class to do formatting for Type2s
 class FileMgrType2 : FileMgr
 {
  public void setnumberFieldsInInput()
  {
   numberFieldsInInput = 15 ;
  }

  override public void Format (string foo)
  {
   fields = foo.Split(new Char[] {','});
   // do a load of formatting here
  }

 }


  class Class1
  {
   /// <summary>
   /// The main entry point for the application.
   /// </summary>
   [STAThread]
   static void Main(string[] args)
   {
    int i;
    const int noFiles = 2;
    string currentRecord;

    // Create an output file here...

    for (i=0; i < noFiles; i++)
    {
     switch (i)
     {
      case 1:
       StreamReader inFile
=new StreamReader("type1.txt", Encoding.ASCII);
       FileMgrType1
fileMgr = new FileMgrType1();
       break;
      case 2:
       StreamReader inFile
=new StreamReader("type2.txt", Encoding.ASCII);
       FileMgrType2
fileMgr = new FileMgrType2();
       break;
     }
     while ((currentRecord =
inFile.ReadLine()) != null)
     {
      fileMgr.Next
(currentRecord);
                                                // Lots of other stuff to
do here
      // Output contents of
fileMgr public members into output file here...
     }
     inFile.Close();
     i++;

    }
   }
  }
 }
}

You can read messages from the DOTNET archive, unsubscribe from DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

Reply via email to