【问题标题】:C# error : CS0103 The name ' ' does not exist in the current contextC# 错误:CS0103 当前上下文中不存在名称“”
【发布时间】:2019-10-12 17:13:54
【问题描述】:

我试图在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;**
}

我在出错的部分加上**。

【问题讨论】:

  • 您的notepad 方法不返回任何值,但其签名表明它应该返回string。在其中添加return 语句。
  • “我把 ** 放在给出错误的部分。” - 你已经指出了代码的多个部分。你问的是哪一个?确切的错误是什么?错误指的是哪一行?请将代码格式化为完整且可编译的代码(当然除了有问题的编译错误),您可以在目标行上使用注释来指示它是哪一个。
  • 如果我要猜测你所指的错误......你试图在你的notepad方法中引用一个名为xe的变量,但是该方法没有这样的变量。
  • 是的,这有点令人困惑,我试图让 xe 进入记事本方法,基本上是试图将一个类调用到另一个方法中?不知道你是不是这么说的。
  • 您的记事本方法正在尝试使用超出范围的对象 (xe)。您必须将 xe 传递给记事本方法,以便它可以对其进行操作。很好奇,如果你只是在方法中向用户询问它(np),为什么你会有一个用于记事本方法的参数(字符串 np)?

标签: c# class methods


【解决方案1】:

我在下面修改了您的代码。 我已经评论了我修改的区域。 有几个方面值得关注。最值得注意的是,您从未调用过记事本方法。 此外,组合输入请求 (convert(getinput)) 不断崩溃。所以,我把它分成两行。

由于您没有从记事本方法返回任何内容,因此我将返回类型更改为 void。然后,我将方法签名从记事本(字符串 np)更改为记事本(用户 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);
        }
    }
}

【讨论】:

  • 那么,既然你添加了 xel,这将是一个与类中的 xe 不同的用户吗?对不起,我在这方面是个菜鸟。
  • xel 是我为签名中的参数起的名字。它在传递时保存该类型的对象。 xel 是 xe 对象的副本。如果你想坚持它,你必须把它归还。
  • Console.WriteLine(np.ToString()); ???您应该Console.WriteLine(np);Console.WriteLine(xel.notepad);
  • 我都做了,这样你就可以看到值被正确传递了。但你不必这样做。
  • 哦,你指的是 .ToString() 部分。这只是我使用 Visual Studio 的习惯。
【解决方案2】:

您无法从方法notepad 访问变量xe,因为它来自方法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;
}

希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 2019-01-05
    • 2021-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多