Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c#对象不';不存在于当前环境中_C#_Constructor - Fatal编程技术网

c#对象不';不存在于当前环境中

c#对象不';不存在于当前环境中,c#,constructor,C#,Constructor,免责声明:我是C#的新手。我很可能做错了什么事,但我不知道用谷歌搜索什么,所以我希望有人能告诉我我做错了什么 无论如何,我正在用我的构造函数在createPlayer方法中创建一个player对象,我可以打印所创建对象的值,但是之后我不能再打印它了,它说: player1在当前上下文中不存在 我该怎么办 主文件 建造师 player1在createPlayer()中实例化,因此其作用域在该函数的本地。如果您想让其他函数访问它,可以使createPlayer()returnplayer1 stat

免责声明:我是C#的新手。我很可能做错了什么事,但我不知道用谷歌搜索什么,所以我希望有人能告诉我我做错了什么

无论如何,我正在用我的构造函数在
createPlayer
方法中创建一个player对象,我可以打印所创建对象的值,但是之后我不能再打印它了,它说:

player1在当前上下文中不存在

我该怎么办

主文件 建造师
player1
createPlayer()
中实例化,因此其作用域在该函数的本地。如果您想让其他函数访问它,可以使
createPlayer()
return
player1

static void Main(string[] args)
{
    var player1 = createPlayer();

    Console.WriteLine(player1.Name);
    Console.ReadKey();
}
static public Player createPlayer()
{
    Console.WriteLine("\nType in your name :");
    Player player1 = new Player(Console.ReadLine()); 
    Console.WriteLine("\n Name: " + player1.Name + "\n Speed: " + player1.Speed + "\n Defence: " + player1.Defence + "\n Damage: " + player1.Damage);
    Console.WriteLine("\nPress 1 to continue, Press 2 to reroll.");

    return player1;
}
垃圾收集器(GC)将在
控制台.WriteLine(player1.Name)中处理作用域末尾的
player1
对象
player1
未处于活动状态

您需要为您的
Player
实例创建
public
属性。您可以按照此代码解决问题

namespace project
{
 class Program
 {
    public static Player player1{get;set;}
    static void Main(string[] args)
    {
        createPlayer();
        Console.WriteLine(player1.Name); 
        Console.ReadKey();
    }
    public static void createPlayer()
    {
        Console.WriteLine("\nType in your name :");
        player1 = new Player(Console.ReadLine());
    .
    .
    .
    }
}

问题是您在createPlayer()方法中创建了Player的对象,因此Player的对象范围将仅限于该块。所以你不能在这个方法之外使用它

好吧,那你可以像这样做

*

*


因此,您的createPlayer方法将返回您创建的当前对象,您将能够在main中使用它。

非常感谢,我以为它是这样的,但对于如何解决这个问题没有真正的线索。虽然是这样,但这并不是世界末日。也许有办法像其他人那样解决这个问题?否则,您只是在诊断,而不是证明解决方案。祝你一切顺利是的,但根据编码,它看起来像是初学者,所以在这个阶段,我们必须给出适当的答案,这个问题可以理解。你错过了重点,也有机会获得投票。很高兴能得到错误的解释(正如您所做的那样),不过最好能说明如何修复它。
player1
被“处置”并不是真的。它被标记为受制于GC sure。没有对
Dispose()
的显式调用,也没有使用(){}
,但是它会被立即处理。
createPlayer()
Main()
是静态的,因此无法访问非静态成员
Player1
。此外,财产名称不应大写。
static void Main(string[] args)
{
    var player1 = createPlayer();

    Console.WriteLine(player1.Name);
    Console.ReadKey();
}
static public Player createPlayer()
{
    Console.WriteLine("\nType in your name :");
    Player player1 = new Player(Console.ReadLine()); 
    Console.WriteLine("\n Name: " + player1.Name + "\n Speed: " + player1.Speed + "\n Defence: " + player1.Defence + "\n Damage: " + player1.Damage);
    Console.WriteLine("\nPress 1 to continue, Press 2 to reroll.");

    return player1;
}
namespace project
{
 class Program
 {
    public static Player player1{get;set;}
    static void Main(string[] args)
    {
        createPlayer();
        Console.WriteLine(player1.Name); 
        Console.ReadKey();
    }
    public static void createPlayer()
    {
        Console.WriteLine("\nType in your name :");
        player1 = new Player(Console.ReadLine());
    .
    .
    .
    }
}
static void Main(string[] args)
        {
             var player1 =   createPlayer();
            Console.WriteLine(player1.Name); //Doesn't exist in current context
            Console.ReadKey();
        }
        static public Player createPlayer()
        {
            Console.WriteLine("\nType in your name :");
            Player player1 = new Player(Console.ReadLine()); 
            Console.WriteLine("\n Name: " + player1.Name + "\n Speed: " + player1.Speed + "\n Defence: " + player1.Defence + "\n Damage: " + player1.Damage);
            Console.WriteLine("\nPress 1 to continue, Press 2 to reroll."); return player1;
        }