How to access an object at point c outside my method?

I am completely new to C #, WPF and OOP, and am facing a problem with objects.

I am creating a small RPG and have a Player class that has a constructor that automatically assigns default values ​​to the object that was created.

So, with WPF / XAML, I created a button associated with a function; when I press the button, this function will be executed:

private void NewGameClick(object sender, RoutedEventArgs e)
{
    Player hero = new Player("Tristan");

    MessageBox.Show("The character " + hero.Name + " has been created.");
}

The character name is actually displayed in the message field, but I cannot use the "hero" object created by the function in the rest of my code. Is it because the object is deleted at the end of the function? Is there a way to save this object and use it outside of this function?

Thank!

+4
source share
6

hero NewGameClick .

, . .

public class Game 
{
    Player hero = new Player("Tristan");

    private void NewGameClick(object sender, RoutedEventArgs e)
    {       
        MessageBox.Show("The character " + hero.Name + " has been created.");
    }
}

private Player class .

+9

, . - .

+1

.

+1

, , ( - ). , , - . , (.. ), (.. , )

+1

hero . .

0

All Articles