C#错误:CS0103名称'';在当前上下文中不存在
我试图在C#错误:CS0103名称'';在当前上下文中不存在,c#,class,methods,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-----
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方法。 此外,组合输入请求(convert(getinput))不断崩溃。所以,我把它分成两行 因为您没有从记事本方法返回任何内容,所以我将返回类型更改为void。然后,我将方法签名从notepad(stringnp)更改为notepad(userxel)。这会将xe对象传递给记事本方法 我添加了一些writeline来显示代码中的位置
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);
}
}
}
无法从方法
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;
}
希望能有帮助 您的
记事本
方法不返回任何值,但其符号建议它应该返回字符串
。在其中添加return
语句。“我将**放在给出错误的部分上。”-您已经指出了代码的多个部分。你问的是哪一个?确切的错误是什么?错误指的是哪一行?请将代码格式化为完整且可编译的代码(当然,除了有问题的编译错误),您可以在目标行上使用注释来指示它是哪一个。如果我猜到您所指的错误。。。您试图在notepad
方法中引用一个名为xe
的变量,但该方法没有这样的变量。是的,这有点令人困惑,我试图将xe引入notepad方法,基本上是试图将一个类调用到另一个方法中?我不知道你是不是这么说的。你的记事本方法试图使用一个超出范围的对象(xe)。您必须将xe传递给notepad方法,以便它可以对其进行操作。奇怪的是,如果你只想在方法内部向用户索要参数(np),为什么你会在记事本方法中使用参数(字符串np)?既然你添加了xel,那么这个用户会与类中的xe不同吗?很抱歉,我在这方面是个笨蛋。xel是我为签名中的参数所起的名字。它在传递时保存该类型的对象。xel是xe对象的副本。如果要持久化它,必须返回它。Console.WriteLine(np.ToString())代码>???您应该选择Console.WriteLine(np)代码>或控制台写入线(像素记事本)代码>我都做了,这样您就可以看到值被正确地传递了。但是你不必。哦,你指的是.ToString()部分。这只是我的一个习惯,因为我经常使用VisualStudio。