Error "The name 'xxx' does not exist in the current context"

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • eschwartz
    New Member
    • Mar 2013
    • 1

    Error "The name 'xxx' does not exist in the current context"

    Within a switch function in a method, I get an error "The name 'Radius' does not exist in the current context." The varible is assigned elsewhere in the code. I don't get the error for the other variables.



    Code:
    public static double MenuItem(int amount)  // method based on menu selection
            {
                double Volume;
                // double Radius;
                double Number = Radius;
    
               // if (amount < 1 || amount > 3)
                 //   amount = 4;
    
                switch (amount)
                {
                    case 1:
                        Volume = 4 / 3 * Math.PI * Math.Pow (Radius, 3);
                        break;
                    case 2:
                        double Length, Width, Height;
                        Volume = Length * Width * Height;
                        break;
                    case 3:
                        Volume = Math.PI * Math.Pow (Radius, 2) * Height;
                        break;
                    default:
                        Console.WriteLine("Your response was out of the expected range");
                        Volume = 0;
                        //Radius = 1;
                        //Length = 1;
                        //Width = 1;
                        //Height = 1;
                        break;
                }
                Console.WriteLine("");
                return Volume;
                }
    Last edited by Rabbit; Mar 3 '13, 01:44 AM. Reason: Please use code tags when posting code.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    We need to see how and where it is declared and populated.

    Comment

    • eschwartz67
      New Member
      • Mar 2013
      • 1

      #3
      Here is the code after I have made some changes. Now the error I am getting is "Use of unassigned local variable 'Volume.'

      Code:
        Console.WriteLine("This program will compute the volume of a 3-D shape.");
      
                  double Volume;
                  
                  SkipLine();
         
                  Console.WriteLine("Computation Menu");
                  SkipLine();
                  Console.WriteLine("1  Volume of a Sphere");
                  Console.WriteLine("2  Volume of a Rectangular Prism");
                  Console.WriteLine("3  Volume of a Cylinder");
                  SkipLine();
      
                  int Response = AskUserForNumber("the number corresponding to the 3-D shape volume you want to       compute:");
                  
                  do
                  {
                      
                  if (Response < 1 || Response > 3)   // is the entry between 1 and 3?
                  {
                      SkipLine();
                      Console.WriteLine("The number you entered is out of the expected range");
                      SkipLine();
                      Response = AskUserForNumber("the number corresponding to the 3-D shape volume you want to compute:");
                      SkipLine();
                  }
                  } while (Response < 1 || Response > 3); // Keep going until Response is between 1 and 3
                  
                  
                 // if (Response == 1)
                  //{
                  //    double SphereRadius = AskUserForNumber("the radius of the Sphere:");
                  //}
                              
                  //if (Response == 2)
                  //{
                   //   double PrismLength = AskUserForNumber("the length of the Rectangular Prism:");
                  //}
                  //if (Response == 2)
                  //{
                   //   double PrismWidth = AskUserForNumber("the width of the Rectangular Prism:");
                  //}
                  //if (Response == 2)
                  //{
                   //   double PrismHeight = AskUserForNumber("the height of the Rectangular Prism:");
                  //}
                 // if (Response == 3)
                  //{
                   //   double CylinderRadius = AskUserForNumber("the radius of the Cylinder:");
                  //}
                  //if (Response == 3)
                  //{
                  //    double CylinderHeight = AskUserForNumber("the height of the Cylinder:");
                  //}
      
                  SkipLine();
                  
                  MenuItem(Response);
      
                  SkipLine();
                  SkipLine();
      
                 // Console.WriteLine(Length + " " + Width + " " + Height + " " + Radius);
      
                  //double Answer = Math.Pow (Length, 3);
                  //double Answer = Length * Length * Length;
                  
                  //Console.WriteLine("Answer is " + Answer);
      
                  Console.WriteLine("The volume of the 3-D shape you selected is " + Volume);
      
                  SkipLine();
                  
                  EndProgram();
              }
      
              public static int AskUserForNumber(string specificQuestion)
              {
                  Console.WriteLine("Please enter {0}", specificQuestion);
                  string theirInput = Console.ReadLine();
                  int aIntValue = Convert.ToInt32(theirInput);
                  return aIntValue;
              }
      
      
              public static double MenuItem(int amount)  // method based on menu selection
              {
                  double Volume = 1;
                  switch (amount)
                  {
                      case 1:
                          double SphereRadius = AskUserForNumber("the radius of the Sphere:");
                          Volume = 4 / 3 * Math.PI * Math.Pow(SphereRadius, 3);
                          break;
                      case 2:
                          double PrismLength = AskUserForNumber("the length of the Rectangular Prism:");
                          SkipLine();
                          double PrismWidth = AskUserForNumber("the width of the Rectangular Prism:");
                          SkipLine();
                          double PrismHeight = AskUserForNumber("the height of the Rectangular Prism:");
                          SkipLine();
                          Volume = PrismLength * PrismWidth * PrismHeight;
                          break;
                      case 3:
                          double CylinderRadius = AskUserForNumber("the radius of the Cylinder:");
                          SkipLine();
                          double CylinderHeight = AskUserForNumber("the height of the Cylinder:");
                          SkipLine();
                          Volume = Math.PI * Math.Pow(CylinderRadius, 2) * CylinderHeight;
                          break;
                      default:
                          Console.WriteLine("Your response was out of the expected range");
                          Volume = 0;
                          //Radius = 1;
                          //Length = 1;
                          //Width = 1;
                          //Height = 1;
                          break;
                  }
                  Console.WriteLine("");
                  return Volume;
                  }
      
              public static void HitAnyKeyToContinue()
              {
                  Console.WriteLine("Hit any key to continue. . .");
                  Console.ReadKey();
                  Console.WriteLine("");
              }
      
              public static void EndProgram()
              {
                  Console.WriteLine("Press any key to end program...");
                  Console.ReadKey();
              }
      
              public static void SkipLine()
              {
                  Console.WriteLine("");
              }
          }
      The erro is in this line:

      Console.WriteLi ne("The volume of the 3-D shape you selected is " + Volume);
      Last edited by Niheel; Mar 3 '13, 03:42 AM. Reason: added code tags

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        Please use code tags when posting code.

        The problem is that you never assign a value to your volume variable on line 3 in whatever that function is.

        Comment

        • M1kkelZU
          New Member
          • Feb 2013
          • 80

          #5
          So basically you would need to assign a base value, am I right Rabbit?
          so something like
          Code:
          double Example = 0;

          Comment

          • Rabbit
            Recognized Expert MVP
            • Jan 2007
            • 12517

            #6
            Yes. That is the crux of the problem. But after you fix that problem, you will notice a new one. That volume isn't getting set when you call your MenuItem function. And that's because you're not assigning the return value to the varible on line 58.

            Comment

            Working...