2 Replies - 551 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");
            string inputString;
            inputString = theTextFile.ReadLine();
            if (inputString != null)
        } while (inputString != null);

    }//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 online

  • Suitor #2
  • member icon

Reputation: 10098
  • View blog
  • Posts: 38,685
  • 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 offline

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

Reputation: 5859
  • View blog
  • Posts: 12,756
  • 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

           string Ranger = "Smith"; // Accessible to the loop

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1