简体   繁体   中英

Why can't I execute the Method here in C#

if (titleGiven)
{
    Console.WriteLine("Enter a title:");
    title = Console.ReadLine(); 
    Post firstPost = new Post(title, username, isPublic);
}
else
{
    Post firstPost = new Post();
}
    
while (end)
{
    Console.WriteLine("Do you want to check the information about the post? Yes|No");
    checkPost = Console.ReadLine(); 
    if (checkPost == "yes")
    {
        firstPost.ShowInfo(); 
    }
                        
}

So basically when I enter a title, a new object of the class Post is being made. This class has a method called ShowInfo() which displays the ID, the title, the user who posted the post etc. So in my while I check if the user wants to check the post info, if that is the case the ShowInfo Method of the class Post should show up. But it doesn't let me use the Method, because there is no such thing as a "firstPost" in this context.

This is all about scopes. The easiest way in your example to understand the problem is that: scope of variable is limited by braces {} where variable is defined.

So your variable is in only in if and corresponding else scope. In order for it to be visible in other "sub-scopes", you need to declare it outside if :

Post firstPost;
if (titleGiven)
{
    Console.WriteLine("Enter a title:");
    title = Console.ReadLine(); 
    firstPost = new Post(title, username, isPublic);
}
else
{
    firstPost = new Post(); 
}

You're going to want to declare first post outside of the if/else statements, and then set them within the if/else statements:

Post firstPost = default;
if (titleGiven)
            {
                Console.WriteLine("Enter a title:");
                title = Console.ReadLine(); 
                firstPost = new Post(title, username, isPublic);
            }
            else
            {
                firstPost = new Post(); 
            }

            while(end)
            {
                Console.WriteLine("Do you want to check the information about the post? Yes|No");
                checkPost = Console.ReadLine(); 
                if (checkPost == "yes")
                {
                    firstPost.ShowInfo(); 
                }
                    
            }

The reason why has to do with the scope of the variable that you're declaring. See more here: https://stackoverflow.com/questions/17444020/why-cant-variables-be-declared-in-an-if-statement#:~:text=variable%20'a'%20can%20be%20accessed,have%20declare%20variable%20'a'.

firstPost is unknown to the while loop because it is out of scope

I suggest doing it like this (I did not test this):

Post firstPost;
if (titleGiven)
{
    Console.WriteLine("Enter a title:");
    title = Console.ReadLine();
    firstPost = new Post(title, username, isPublic);
}
else
{
    firstPost = new Post();
}

while (end)
{
    Console.WriteLine("Do you want to check the information about the post? Yes|No");
    checkPost = Console.ReadLine();
    if (checkPost == "yes")
    {
        firstPost.ShowInfo();
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM