Hi everyone....

public void LoopGrades()
        {

            try//Start try block
            {
                //Start TextReader
                TextReader grades = new StreamReader("grades.dat");
               ResultOfData = grades.ReadLine();
            }
            catch (FileNotFoundException e)
            {
                MessageBox.Show("File Not Found!", "Error");
                
            }
           
                //Loop through grades 
                while (ResultOfData != null)
                {
                    
                    //ResultOfData is given the value that is read from grades.dat
                    ResultOfData =  grades.ReadLine();
                    //Call IncrementGrades method
                    IncrementGrades();
                }
                grades.Close();

My question is...grades has gone out of scope..could anyone please tell me how I can gain access to it from the while loop??

ResultOfData is declared in the class if anyone was wondering whats going on there......

Thanks for your help...

Recommended Answers

All 2 Replies

Hi everyone....

public void LoopGrades()
{

try//Start try block
{
//Start TextReader
TextReader grades = new StreamReader("grades.dat");
ResultOfData = grades.ReadLine();
}
catch (FileNotFoundException e)
{
MessageBox.Show("File Not Found!", "Error");

}

//Loop through grades
while (ResultOfData != null)
{

//ResultOfData is given the value that is read from grades.dat
ResultOfData = grades.ReadLine();
//Call IncrementGrades method
IncrementGrades();
}
grades.Close();


My question is...grades has gone out of scope..could anyone please tell me how I can gain access to it from the while loop??

ResultOfData is declared in the class if anyone was wondering whats going on there......

Thanks for your help...

I think when a variable is declared inside a try/catch it's scope is the try/catch...
So probably by declaring

TextReader grades;

before the try/catch problem should be solved?

hmmm..thanks for your reply...although this has not solved it...i want to gain access to the existing grades variable from the try block after the previous one as it would give better exception handling

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.