2 Replies - 302 Views - Last Post: 27 March 2013 - 05:42 PM Rate Topic: -----

#1 wolverineandrew  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 24-January 13

Error 'the name doesnt exist in the current context'

Posted 27 March 2013 - 05:25 PM

Can some one help me understand why im getting an error on the inputString on my line of the while statement? This is code copied straight from my professor given to us to use and its giving me an error. The error says "The name 'inputString' does not exist in the current context". All the other times its used in this code its fine, just the error on that one. Please help me understand why this is happening. Thank you
 using System;
using System.IO;

class Program
{
    const int SIZE = 100;
    static void Main()
    {

        int[] arrayName;
        arrayName = new int[SIZE];

        StreamReader theTextFile;
        theTextFile = new StreamReader("grades.txt");
        do
        {
            string inputString;
            inputString = theTextFile.ReadLine();
            if (inputString != null)
                Console.WriteLine(inputString);
        } while (inputString != null);

        Console.ReadLine();
    }//End Main()
}//End class Program  


Is This A Good Question/Topic? 0
  • +

Replies To: Error 'the name doesnt exist in the current context'

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8365
  • View blog
  • Posts: 31,080
  • Joined: 12-June 08

Re: Error 'the name doesnt exist in the current context'

Posted 27 March 2013 - 05:33 PM

'Code help' doesn't really fall under 'advanced'.. moving to gen-pop help.
Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5316
  • View blog
  • Posts: 11,355
  • Joined: 02-June 10

Re: Error 'the name doesnt exist in the current context'

Posted 27 March 2013 - 05:42 PM

You defined inputString within the do{} block.

Line 21: Trying to access it outside the do{} block means it doesn't exist there.

I'm sure if you look at your other uses of this block you'd find line 17 creating the string is actually BEFORE the line do

Its what we call "scope". A thing exists between the {} it was created in. So if you make a thing within a for{} loop, that's its scope. If you want to access a thing for the entire class, then define it in the class braces

class someClass
{
   int yogi = 5; // Accessible to the class
   
   void someMethod()
   {
        int booboo = 3; // Accessible to the method

        while(true)
        {
           string Ranger = "Smith"; // Accessible to the loop
        }
   }
}

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1