Skip to content

The name '____' does not exist in current context.

Elizabeth Foster

Asked Nov 4, 2015, 8:16 PM EST1 answer
Share
LinkedInX
 There areas that this error is occurring is highlighted in yellow (there are only three). 
 
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace TaskoneBinaryConverstion
{
    class Program
    {
        static string userNumber = "";
        static int answer = 0;
        static Boolean userInputOk = false;
        static void Main()
        {
             do
            {
                Console.WriteLine("This program will convert binary to decimal");
                //Give user instructions
                Console.WriteLine("Please enter a binary number");

                //read in the user binary number assign it to a variable
                userNumber = Console.ReadLine();
                if (userNumber != "")
                {
                    userInputOk = true;

                    foreach (char currentChar in userNumber)
                    {
                        //checks individual digit to ensure it is either a 1 or 0
                        if (currentChar != '1' && currentChar != '0')
                        {
                            userInputOk = false;
                            Console.Clear();
                            Console.WriteLine("An error has occurred, please only use 1 or 0");

                        }
                        {
                        }
                    }
                }
                else
                    //if the user did not input either a one or zero the program will not preform the function and
                    //will re-iterate the question
                    userInputOk = false;
                {
                }
            }

            while (userInputOk == false);
             // end loop when all digits have been delt with
             //display convereted number
             currentMultiplicationFactor();
            Console.WriteLine("Answer= " + answer);
             //prevent console from closing
             Console.ReadKey();
            //prevents program from crashing

            //loop through the given number (string)
            //starting from the last number down to the first number

            }
             //read from file method... this is the method which gets called if a valid file is found at the beginning.
             private static void readFromFile()
            {
            //readFromFile binary lines from set file
            String[] tempCodes = File.ReadAllLines(armCodeFile);
            //execute the following code for each line of the file
            foreach (string currentcode in tempCodes)
            {
                //set the code to the usernumber variable
                UserNumber = currentcode;
                Calculatedecimal();
                //display the answer
                Console.WriteLine(currentcode + " To decimal = " + answer);
            }
        }


            private static void currentMultiplicationFactor()
            {
                answer = 0;
                int currentMultiplicationFactor = 1;
               for (int index = userNumber.Length - 1; index > -1; index--)
            {
                if (userNumber[index] == '1')
                {
                    answer += currentMultiplicationFactor;
                }
                    currentMultiplicationFactor *= 2;

                 }
              }
            }
         }

1 Answer

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

  • Shashank Tripathi
    0
    Their are probably following reason ...
    i) You have declare userNumber but you are using as UserNumber
    ii) It might possible that your application is not able to build your solution if you have more than one directory.
     
      Clean your solution and rebuild it.
     
    It might work.
     
    Second approach is tested as I also faced this issue 
    0