Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7679705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:56:22+00:00 2026-05-31T17:56:22+00:00

This kind of code would normally work in PHP, but since the scope is

  • 0

This kind of code would normally work in PHP, but since the scope is much more strict in C#, it’s not. I can’t figure out a way to write this code without repeating myself.

    static double Cube()
    {
        Console.Write("Enter the side length of the cube: ");
        try
        {
            double x = Convert.ToDouble(Console.Read());
            return Math.Pow(x, 3);
        }
        catch (FormatException)
        {
            Console.WriteLine("Invalid input, please enter a number.");
            Cube();
        }
        return 1;
    }

..Later in Main():

            switch (choice)
            {
                case 0:
                    return;
                case 1:
                    double final = Cube();
                    break;
                default:
                    Console.WriteLine("Please enter 0 or 1.");
                    Main();
                    break;
            }
            Console.WriteLine("The volume is: {0}", Convert.ToString(final));

The Cube() method works fine, but it’s messy in my opinion (return 1 at the end to make the compiler happy). But an error comes up saying The name ‘final’ does not exist in the current context. It can’t find final. So the only way to make this work that I’m seeing is to put the Console.WriteLine statement right after the double final = Cube().

I’ve also tried declaring double final; outside the switch, then just setting final inside each case, but that hasn’t worked either.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T17:56:24+00:00Added an answer on May 31, 2026 at 5:56 pm

    If you want to access final from outside the switch scope, you’ll have to declare it outside that scope too. If you reference final and there are code paths that allow not setting a value to final, then the compiler will be “angry”.

    In php, final would magically be 0 when you don’t assign anything to it. Try declaring final before the switch, and then assign a value to it at each case statement including the default case.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This feels like the kind of code that only fails in-situ, but I will
This is kind of a brainteaser question, since the code works perfectly fine as-is,
Can you please tell me what kind of construct in C# is this. Code
I have this kind of code in some applications (from microsoft) [assembly: System.Diagnostics.CodeAnalysis.SuppressMessage( Microsoft.Naming,
Just wondering is this kind of code recommended to increase performance? void functionCalledLotsofTimes() {
I'm using this kind of code for my TCP/IP connection: sock = new Socket(host,
When declaring a template, I am used to having this kind of code: template
I have a bunch of occurrences of this kind of boilerplate code in my
What does this bit of code represent? I know it's some kind of if
This question is kind of lengthy but I try to provide you with the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.