Skip to content

Filestream object not in scope, need work around ideas

doug 0

Asked Sep 2, 2005, 8:49 AM EDT3 answers
Share
LinkedInX
Hi everyone, My problem exists with creating a Filestream and BinaryWriter objects in a C# app that collects real time data very quickly. I only collect want to create the file if a specific sensor is being used. However, C# does not allow the the creation of either of these objects with the ability to use them later on in the method because the specific object was not initialized. Let me give a sample piece of code...
private void Collect_data()
{ 
 int y_1 =0; //collected integer value to be written to file 
 
 if(sensor1_on ==true) 
 { 
   FileStream writeMyFileGPS = new FileStream(fileNameGPS, FileMode.Append,FileAccess.Write);
   BinaryWriter sr= new BinaryWriter(writeMyFileGPS);
 } 

  //WRITE COLLECTED DATA TO FILE IF USING THIS SENSOR 
  if(sensor1_on==true)  
  { 
    sr.BaseStream.WriteByte((byte)(y_1 & 0x000000FF));
   } 

   //END OF METHOD CLOSE HANDLERS 
   if(sensor1_on==true)  
   { 
     sr.Close();
     writeMyFileGPS.Close();
   }

}//END-COLLECT DATA METHOD
The problem lies in that C# will not let you create the object in the if statement and then use it later in the method. It does not have scope outside the if statement in which it was created (CORRECT???) (you could do this in C).
 
Now, I could create the objects within the if statements where I write the binary data to the file, but this takes an eternity about 0.001 seconds every time I get data in and it kills the throughput in my app. I don't want to create the the file if I don't have to so any work arounds would be much appreciated. Thank you...

3 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0

    Would help if you declare the file streams prior to your code blocks?

    The following code declares your file streams but does not instantiate 
    them until you want them. It would also give your stream the scope that you need.

    private void Collect_data()
    { 
     int y_1 =0; //collected integer value to be written to file 
     FileStream writeMyFileGPS;
     BinaryWriter sr;

     if(sensor1_on ==true) 
     { 
       writeMyFileGPS = new FileStream(fileNameGPS, FileMode.Append,FileAccess.Write);
       sr= new BinaryWriter(writeMyFileGPS);
     } 

      //WRITE COLLECTED DATA TO FILE IF USING THIS SENSOR 
      if(sensor1_on==true)  
      { 
        sr.BaseStream.WriteByte((byte)(y_1 & 0x000000FF));
       } 

       //END OF METHOD CLOSE HANDLERS 
       if(sensor1_on==true)  
       { 
         sr.Close();
         writeMyFileGPS.Close();
       }

    }

    0
  • 0
    Thanks Crausch for the reply, appreciate it.

    Unfortunately your suggestion has the same problem, C# will not let you instantiate the 
    object within the if statement and use it later, even if the objects are declared early.  

    The end of the if statement appears to be the end of the objects scope.  I tried your code 
    in a quick app and had the same compile errors, but as soon as I instantiated the objects
    within each of the if statements it worked.  

    Thanks again...
    0
  • 0

    Since it looks as though each usage is within a condition of the sensor1_on being "true", 
    could you not place all the code for "true" in the same conditional test?

    How about something like this...

    private void Collect_data()
    { 
     int y_1 =0; //collected integer value to be written to file 
     
     if(sensor1_on ==true) 
     { 
         FileStream writeMyFileGPS = new FileStream(fileNameGPS, FileMode.Append,FileAccess.Write);
         BinaryWriter sr= new BinaryWriter(writeMyFileGPS);
     
        //WRITE COLLECTED DATA TO FILE IF USING THIS SENSOR 
        sr.BaseStream.WriteByte((byte)(y_1 & 0x000000FF));
         

         //END OF METHOD CLOSE HANDLERS 
          sr.Close();
           writeMyFileGPS.Close();
        
       }

    }//END-COLLECT DATA METHOD

    0