为什么我的实例在当前上下文中不存在?[已结束]

Why does my instance not exist in the current context? [closed]

提问人:Calcul4tor 提问时间:4/8/2023 最后编辑:marc_sCalcul4tor 更新时间:4/8/2023 访问量:102

问:


编辑问题以包括所需的行为、特定问题或错误以及重现问题所需的最短代码。这将有助于其他人回答这个问题。

8个月前关闭。

我使用了 if 语句来确定实例应该使用哪个构造函数,但它声称它在当前上下文中不存在。我使用了应该获得全名、眼睛颜色和年龄的方法,但两者都是可选的。然后,它应根据用户输入确定应使用哪个构造函数。GetDetails

之后,它将调用函数 ,它只是自我介绍。此函数位于另一个名为 的类中,该类包含所有构造函数。代码尚未完全完成,仍然存在错误,但是我想了解此错误是什么以及如何修复它。IntroduceMyselfHuman

这是主类:

class Program
{
    static string fullName;
    static string eyeColour;
    static int age;

    static void Main(string[] args)
    {
        GetDetails();
        
        if (eyeColour == "")
        { 
            Human person1 = new Human(fullName); 
        }
        else if (eyeColour != "")
        { 
            Human person1 = new Human(fullName, eyeColour); 
        }
        else
        { 
            Human person1 = new Human(fullName, eyeColour, age); 
        }

        person1.IntroduceMyself(fullName, eyeColour, age);
    }

    public static void GetDetails()
    {    
        Console.Write("Hello! To start, please enter your full name:");
        fullName = Console.ReadLine();

        Console.Write("Would you like to input your eye colour? y / n :");
        if (Console.ReadLine() == "y")
        {
            Console.Write("Please enter your eye colour:");
            eyeColour = Console.ReadLine();
        }

        Console.Write("Would you like to input your age? y / n :");
        if (Console.ReadLine() == "y")
        {
            Console.Write("Please enter your age:");
            while(!(Int32.TryParse(Console.ReadLine(), out age)))
            {
                Console.Write("There was an error! Please try again:");
            }
        }
    }

这是人类类:

internal class Human
{
    public string fullName;
    public string eyeColour;
    public int age;

    public Human(string fullName)
    {
        this.fullName = fullName;
    }

    public Human(string fullName, string eyeColour)
    {
        this.fullName = fullName;
        this.eyeColour = eyeColour;
    }

    public Human(string fullName, string eyeColour, int age)
    {
        this.fullName = fullName;
        this.eyeColour = eyeColour;
        this.age = age;
    }

    public static void IntroduceMyself(string fullName, string eyeColour, int age)
    {
        Console.WriteLine("Hello {0}, you have {1} eyes and you are {2} years old", fullName, eyeColour, age);
    }
}
C# 方法 构造函数

评论

0赞 jdweng 4/8/2023
它是静态的。未在实例中引用的静态对象。相反,您可以使用类名直接访问:Human.IntroduceMyself()
0赞 Jon Skeet 4/8/2023
欢迎来到 Stack Overflow。“我想了解这个错误是什么”——确切地说是什么错误?你还没告诉我们怎么了。此外,如果您呈现的代码存在多个错误,但您只对其中一个错误感兴趣,那真的无济于事。如果您提供一个代码示例,该示例仅演示您需要帮助的错误,那就更好了。
0赞 Hans Kesting 4/8/2023
仅存在于 if 块的作用域中。你认为你什么时候会到达第二个?Human person1

答:

3赞 Milly Stack 4/8/2023 #1
  1. 该变量在语句内部声明,不能在相应块的范围之外访问。因此,不能在这些块之外访问该变量以调用 IntroduceMyself 方法。person1if-elseif-elseperson1

  2. 该函数被定义为静态函数,这意味着您可以使用类名后跟函数名来调用它,而不需要类 () 的实例。我建议删除静电。IntroduceMyselfHuman.IntroduceMyself(fullName, eyeColor, age)

下面是更新后的代码

// Human class
internal class Human
{
    public string fullName;
    public string eyeColour;
    public int age;

    
    public Human(string fullName)
    {
        this.fullName = fullName;
    }
    public Human(string fullName, string eyeColour)
    {
        this.fullName = fullName;
        this.eyeColour = eyeColour;
    }
    public Human(string fullName, string eyeColour, int age)
    {
        this.fullName = fullName;
        this.eyeColour = eyeColour;
        this.age = age;
    }
    // Function no longer static. Also removed the parameters since they are apart of the object
    public void IntroduceMyself()
    {
        
        Console.WriteLine("Hello {0}, you have {1} eyes and you are {2} years old", fullName, eyeColour, age);
    }
}
using System;
class Program
{
    static string fullName;
    static string eyeColour;
    static int age;

    static void Main(string[] args)
    {
        GetDetails();
        // defining the person 
        Human person1 = null;
        if (eyeColour == "")
        { 
             person1 = new Human(fullName); 
        }
        else if (eyeColour != "")
        { 
             person1 = new Human(fullName, eyeColour); 
        }
        else
        { 
             person1 = new Human(fullName, eyeColour, age); 
        }

        person1.IntroduceMyself();
    }

    public static void GetDetails()
    {    
        Console.Write("Hello! To start, please enter your full name:");
        fullName = Console.ReadLine();

        Console.Write("Would you like to input your eye colour? y / n :");
        if (Console.ReadLine() == "y")
        {
            Console.Write("Please enter your eye colour:");
            eyeColour = Console.ReadLine();
        }

        Console.Write("Would you like to input your age? y / n :");
        if (Console.ReadLine() == "y")
        {
            Console.Write("Please enter your age:");
            while(!(Int32.TryParse(Console.ReadLine(), out age)))
            {
                Console.Write("There was an error! Please try again:");
            }
        }
    }
}

评论

0赞 Luuk 4/8/2023
请注意,如果 eyeColour 为 null:i.stack.imgur.com/bFn5x.png