C#错误:CS0103名称''在当前上下文中不存在

时间:2019-10-12 17:13:54

标签: c# class methods

我正在尝试用Main之外的方法调用在Main中创建的类对象,但是它不起作用。我是C#的新手,所以我习惯了python的Def及其方式。

public class User
{
  public string name;
  public string password;
  public string notepad;
}

static void Main(string[] args)
{ 
    Console.Clear();
    Console.WriteLine("\n--------------------------\n"+"Please create a new user."+"\n--------------------------");
    Console.WriteLine("\nUsername: ");
    string x = Console.ReadLine();
    Console.WriteLine("\nPassword: ");
    string y = Console.ReadLine();
    **User xe = new User();
    xe.name = x;**

   ----removed cuz has nothing to do with the code----

    Console.WriteLine("Dashboard");
    Console.WriteLine("\nPlease choose a function by inputting the number before the name.\n1 - Notepad\n2 - Calculator");

    while(login) {
        try{
            int letter = Convert.ToInt32(Console.ReadLine());
            login = false;
            if(letter == 1) {
                Console.WriteLine("Notepad");
            }
            else if(letter == 2) {
                Console.WriteLine("Calculator");
            }
        }
        catch{
            Console.WriteLine("Please input a number.");
        }
    }
}
**public static string notepad(string np){
    np = Console.ReadLine();
    xe.notepad = np;**
}

我在出现错误的部分上加上了**。

2 个答案:

答案 0 :(得分:0)

我已经在下面修改了您的代码。 我已经评论了我修改过的区域。 有几个方面值得关注。最明显的是,您从未调用过记事本方法。 而且,组合的输入请求(convert(getinput))一直崩溃。因此,我将其分为两行。

由于您没有从记事本方法返回任何内容,因此我将返回类型更改为void。然后,我将方法签名从notepad(字符串np)更改为notepad(用户xel)。这会将xe对象传递到记事本方法。

我添加了一些编写行以显示您在代码中的位置。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    public class User
    {
        public string name;
        public string password;
        public string notepad;
    }

    class Program
    {
        static void Main(string[] args)
        {
            // I've added this line to get around the missing login source
            bool login = true;

            Console.Clear();
            Console.WriteLine("\n--------------------------\n" + "Please create a new user." + "\n--------------------------");
            Console.WriteLine("\nUsername: ");
            string x = Console.ReadLine();
            Console.WriteLine("\nPassword: ");
            string y = Console.ReadLine();
            User xe = new User();
            xe.name = x;

            //----removed cuz has nothing to do with the code----

            Console.WriteLine("Dashboard");
            Console.WriteLine("\nPlease choose a function by inputting the number before the name.\n1 - Notepad\n2 - Calculator");

            while (login)
            {
                try
                {
                    // Here I've separated the input request, the combination of the convertion and the input mixed kept crashing.
                    string theletter = Console.ReadLine();
                    int letter = Convert.ToInt32(theletter);

                    login = false;
                    if (letter == 1)
                    {
                        Console.WriteLine("Notepad");

                        // Here I'm calling the notepad method with the xe
                        notepad(xe);
                        Console.WriteLine("\n\n* Ok, I'm back in the Main *\n");
                        Console.ReadLine();
                    }
                    else if (letter == 2)
                    {
                        Console.WriteLine("Calculator");
                    }
                }
                catch
                {
                    Console.WriteLine("Please input a number.");
                }
            }
        }

        // **** Here I've changed the return type from string to void, since it's not returning anything.
        // **** I've also added a parameter to pass the xe as xel (optional) using the proper type
        public static void notepad(User xel)
        {
            Console.WriteLine("\n\n * I'm here in the notepad method *\n");
            string np = Console.ReadLine();
            xel.notepad = np;
            Console.WriteLine("\nYou entered this for your notepad: \n");
            Console.WriteLine(np);
            Console.WriteLine("\nThis is your new notepad: \n");
            Console.WriteLine(xel.notepad);
        }
    }
}

答案 1 :(得分:0)

您无法从方法xe访问变量notepad,因为它来自方法Main()
您可以将xe设置为全局(将其放在Main()方法之外),

您可以将其作为参数传递给notepad()。 像这样:

//changed it to void cause you arent returning any value
//removed string np argument because you were not using its value (you were overriding it before you use it)
public static void notepad(User u){

  string np = Console.ReadLine();
  u.notepad = np;
}

希望有帮助。