"name does not exist in current context" when creating public methods in c#

I’m having an issue with my code. I’m new to C# so if I’m using the wrong terminology please bear with me, but the issue I’m having is that I’m trying to cross-reference ‘acctType’ variable in my OpenAcct() method. I’m getting the error code “name ‘acctType’ does not exist in current context” though I have defined acctType in the Main() method.

I have the OpenAcct() Method set as public, so I thought that meant it could be accessed by other methods. Am I correct in this?
Is there something in my code that’s missing?

Please find code below.

https://www.codecademy.com/workspaces/629a0187df9495557e0d0f11

using System;

namespace Task1Part1
{
    class Program
    {
        public static void Main()
        {
            Console.WriteLine("Press 1 for Current Account");
            Console.WriteLine("Press 2 for Saving Account");
            int acctType = Convert.ToInt32(Console.ReadLine());
        }

        public static void OpenAcct()
            {
                if (acctType == 1)  // this is where I'm getting the error
                {
                    Console.WriteLine("Lets open a savings account.");
                }
            }
        }
    }
}

The issue is that acctType has not been defined inside the scope of the OpenAcct() method. The method being public makes no difference. It looks like the eventual intent will be to call the OpenAcct() method from the Main() method. Since acctType is defined inside the Main() method, you will need to pass it along as an argument when you make the function call, so your OpenAcct() method will need to accept a parameter.

1 Like