473,396 Members | 2,111 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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.



Expand|Select|Wrap|Line Numbers
  1. public static double MenuItem(int amount)  // method based on menu selection
  2.         {
  3.             double Volume;
  4.             // double Radius;
  5.             double Number = Radius;
  6.  
  7.            // if (amount < 1 || amount > 3)
  8.              //   amount = 4;
  9.  
  10.             switch (amount)
  11.             {
  12.                 case 1:
  13.                     Volume = 4 / 3 * Math.PI * Math.Pow (Radius, 3);
  14.                     break;
  15.                 case 2:
  16.                     double Length, Width, Height;
  17.                     Volume = Length * Width * Height;
  18.                     break;
  19.                 case 3:
  20.                     Volume = Math.PI * Math.Pow (Radius, 2) * Height;
  21.                     break;
  22.                 default:
  23.                     Console.WriteLine("Your response was out of the expected range");
  24.                     Volume = 0;
  25.                     //Radius = 1;
  26.                     //Length = 1;
  27.                     //Width = 1;
  28.                     //Height = 1;
  29.                     break;
  30.             }
  31.             Console.WriteLine("");
  32.             return Volume;
  33.             }
Mar 2 '13 #1
5 6028
Rabbit
12,516 Expert Mod 8TB
We need to see how and where it is declared and populated.
Mar 3 '13 #2
Here is the code after I have made some changes. Now the error I am getting is "Use of unassigned local variable 'Volume.'

Expand|Select|Wrap|Line Numbers
  1.   Console.WriteLine("This program will compute the volume of a 3-D shape.");
  2.  
  3.             double Volume;
  4.  
  5.             SkipLine();
  6.  
  7.             Console.WriteLine("Computation Menu");
  8.             SkipLine();
  9.             Console.WriteLine("1  Volume of a Sphere");
  10.             Console.WriteLine("2  Volume of a Rectangular Prism");
  11.             Console.WriteLine("3  Volume of a Cylinder");
  12.             SkipLine();
  13.  
  14.             int Response = AskUserForNumber("the number corresponding to the 3-D shape volume you want to       compute:");
  15.  
  16.             do
  17.             {
  18.  
  19.             if (Response < 1 || Response > 3)   // is the entry between 1 and 3?
  20.             {
  21.                 SkipLine();
  22.                 Console.WriteLine("The number you entered is out of the expected range");
  23.                 SkipLine();
  24.                 Response = AskUserForNumber("the number corresponding to the 3-D shape volume you want to compute:");
  25.                 SkipLine();
  26.             }
  27.             } while (Response < 1 || Response > 3); // Keep going until Response is between 1 and 3
  28.  
  29.  
  30.            // if (Response == 1)
  31.             //{
  32.             //    double SphereRadius = AskUserForNumber("the radius of the Sphere:");
  33.             //}
  34.  
  35.             //if (Response == 2)
  36.             //{
  37.              //   double PrismLength = AskUserForNumber("the length of the Rectangular Prism:");
  38.             //}
  39.             //if (Response == 2)
  40.             //{
  41.              //   double PrismWidth = AskUserForNumber("the width of the Rectangular Prism:");
  42.             //}
  43.             //if (Response == 2)
  44.             //{
  45.              //   double PrismHeight = AskUserForNumber("the height of the Rectangular Prism:");
  46.             //}
  47.            // if (Response == 3)
  48.             //{
  49.              //   double CylinderRadius = AskUserForNumber("the radius of the Cylinder:");
  50.             //}
  51.             //if (Response == 3)
  52.             //{
  53.             //    double CylinderHeight = AskUserForNumber("the height of the Cylinder:");
  54.             //}
  55.  
  56.             SkipLine();
  57.  
  58.             MenuItem(Response);
  59.  
  60.             SkipLine();
  61.             SkipLine();
  62.  
  63.            // Console.WriteLine(Length + " " + Width + " " + Height + " " + Radius);
  64.  
  65.             //double Answer = Math.Pow (Length, 3);
  66.             //double Answer = Length * Length * Length;
  67.  
  68.             //Console.WriteLine("Answer is " + Answer);
  69.  
  70.             Console.WriteLine("The volume of the 3-D shape you selected is " + Volume);
  71.  
  72.             SkipLine();
  73.  
  74.             EndProgram();
  75.         }
  76.  
  77.         public static int AskUserForNumber(string specificQuestion)
  78.         {
  79.             Console.WriteLine("Please enter {0}", specificQuestion);
  80.             string theirInput = Console.ReadLine();
  81.             int aIntValue = Convert.ToInt32(theirInput);
  82.             return aIntValue;
  83.         }
  84.  
  85.  
  86.         public static double MenuItem(int amount)  // method based on menu selection
  87.         {
  88.             double Volume = 1;
  89.             switch (amount)
  90.             {
  91.                 case 1:
  92.                     double SphereRadius = AskUserForNumber("the radius of the Sphere:");
  93.                     Volume = 4 / 3 * Math.PI * Math.Pow(SphereRadius, 3);
  94.                     break;
  95.                 case 2:
  96.                     double PrismLength = AskUserForNumber("the length of the Rectangular Prism:");
  97.                     SkipLine();
  98.                     double PrismWidth = AskUserForNumber("the width of the Rectangular Prism:");
  99.                     SkipLine();
  100.                     double PrismHeight = AskUserForNumber("the height of the Rectangular Prism:");
  101.                     SkipLine();
  102.                     Volume = PrismLength * PrismWidth * PrismHeight;
  103.                     break;
  104.                 case 3:
  105.                     double CylinderRadius = AskUserForNumber("the radius of the Cylinder:");
  106.                     SkipLine();
  107.                     double CylinderHeight = AskUserForNumber("the height of the Cylinder:");
  108.                     SkipLine();
  109.                     Volume = Math.PI * Math.Pow(CylinderRadius, 2) * CylinderHeight;
  110.                     break;
  111.                 default:
  112.                     Console.WriteLine("Your response was out of the expected range");
  113.                     Volume = 0;
  114.                     //Radius = 1;
  115.                     //Length = 1;
  116.                     //Width = 1;
  117.                     //Height = 1;
  118.                     break;
  119.             }
  120.             Console.WriteLine("");
  121.             return Volume;
  122.             }
  123.  
  124.         public static void HitAnyKeyToContinue()
  125.         {
  126.             Console.WriteLine("Hit any key to continue. . .");
  127.             Console.ReadKey();
  128.             Console.WriteLine("");
  129.         }
  130.  
  131.         public static void EndProgram()
  132.         {
  133.             Console.WriteLine("Press any key to end program...");
  134.             Console.ReadKey();
  135.         }
  136.  
  137.         public static void SkipLine()
  138.         {
  139.             Console.WriteLine("");
  140.         }
  141.     }
The erro is in this line:

Console.WriteLine("The volume of the 3-D shape you selected is " + Volume);
Mar 3 '13 #3
Rabbit
12,516 Expert Mod 8TB
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.
Mar 3 '13 #4
M1kkelZU
80 64KB
So basically you would need to assign a base value, am I right Rabbit?
so something like
Expand|Select|Wrap|Line Numbers
  1. double Example = 0;
Mar 7 '13 #5
Rabbit
12,516 Expert Mod 8TB
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.
Mar 7 '13 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: F. Barth | last post by:
Hello, I've posted this problem to one other newsgroups, and gotten some diagnostic help, but I still need a solution. The full text of the message box is: "The field is too small to accept the...
0
by: LordHog | last post by:
Hello all, I would like to use the new SerialPort class in Visual C++ 2005 Express edition, but I am having problems adding my event handler to the DataReceived event. In the header file I have...
2
by: fabrice | last post by:
Hello I'm getting an error during a .vb file compilation. My command is : vbc /t:library /r:system.web.dll /r:system.dll /r:mscorlib.dll myFile.vb The error is :
3
by: divya | last post by:
Hi, I have a table tblbwday with 2 fields Name and Birthday.I have written this script for displaying evryday names of the people on that day. <% set objConn...
2
by: Jeff | last post by:
hey asp.net 2.0 (C'#) In the code behind file I have this method: public String AddBR(Object param) { String text = (String) param; return text; }
3
by: Michael | last post by:
Hi, I am getting a strange error. Last night when I left work this was working perfectly. This morning when I try to run this code in VS2005, it comes up with an error saying "The name 'UserName'...
1
by: William Cruz | last post by:
Can anyone help me with this piece of code. - I keep on getting the error message "The object exporter specified was not found" If i change it to run on my local machine it works fine, the...
1
by: avecreep | last post by:
Hi, i was trying to find a web application and i found one open source on the internet. i put the code folders on the root folder and when i tried to acces via web browser it all worked fine ( i...
0
by: vaibhavbhosale | last post by:
I am using master page. It contains textbox with other controls on it. I can catch textbox name in code behind page but while compiling it gives error like textbox not exists in current context....
1
by: Gozd Arao | last post by:
I have a VB code as follows: Private Shared ReadOnly RowClickedEventKey As Object = New Object Public Custom Event RowClicked As EventHandler(Of GridViewRowClickedEventArgs) AddHandler(ByVal...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.