简体   繁体   English

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

[英]C# error : CS0103 The name ' ' does not exist in the current context

I'm trying to call a class object made in the Main in a method outside of the Main , but it is not working.我正在尝试以Main之外的方法调用在Main中制作的 class object ,但它不起作用。 I'm new to C# so I am used to python's Def and its way different.我是 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;**
}

I put ** on the parts that are giving an error.我把 ** 放在给出错误的部分上。

I've modified your code below.我在下面修改了您的代码。 I've commented the areas that I've modified.我已经评论了我修改的区域。 There were a few areas of concern.有几个方面值得关注。 Most noticeably, you never called the notepad method.最值得注意的是,您从未调用过记事本方法。 Also, the combined input request (convert(getinput)) kept crashing.此外,组合输入请求 (convert(getinput)) 不断崩溃。 So, I broke it up into two lines.所以,我把它分成两行。

Since you weren't returning anything from the notepad method, I changed the return type to void.由于您没有从记事本方法返回任何内容,因此我将返回类型更改为 void。 Then, I changed the method signature from notepad(string np) to notepad(User xel).然后,我将方法签名从记事本(字符串 np)更改为记事本(用户 xel)。 This passes the xe object to the notepad method.这会将 xe object 传递给记事本方法。

I've added some writelines to show where you are in the code.我添加了一些写行来显示您在代码中的位置。

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);
        }
    }
}

You cannot access the variable xe from the method notepad because it is from the method Main() .您无法从方法notepad访问变量xe ,因为它来自方法Main()
You can either make the xe global (putting it outside the Main() method),您可以使xe全局化(将其放在Main()方法之外),
or或者
you can pass it to notepad() as an argument.您可以将其作为参数传递给notepad() Like this:像这样:

//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;
}

hope it helps.希望能帮助到你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#错误CS0103:名称“ i”在当前上下文中不存在 - C# Error CS0103: The name 'i' does not exist in the current context C#错误CS0103当前上下文中不存在名称“ originalImage” - C# Error CS0103 The name 'originalImage' does not exist in the current context 错误 CS0103:当前上下文中不存在名称“_context” - Error CS0103: The name '_context' does not exist in the current context 错误 CS0103:当前上下文中不存在名称“currentScreen”(CS0103) - Error CS0103: The name 'currentScreen' does not exist in the current context (CS0103) 错误CS0103:名称“ TimeSpan”在当前上下文(CS0103)(testingProgram)中不存在? - Error CS0103: The name 'TimeSpan' does not exist in the current context (CS0103) (testingProgram)? 错误cs0103名称'IEnumerator'在当前上下文中不存在 - error cs0103 The Name 'IEnumerator' does not exist in the current context 错误CS0103:名称“ HttpUtility”在当前上下文中不存在 - error CS0103: The name `HttpUtility' does not exist in the current context "错误 CS0103:当前上下文中不存在名称“AssetPreview”" - error CS0103: The name 'AssetPreview' does not exist in the current context 错误CS0103当前上下文中不存在名称“图像” - Error CS0103 The name 'Image' does not exist in the current context Unity错误CS0103:当前上下文中不存在名称`' - Unity error CS0103: The name `' does not exist in the current context
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM