Reputation: 23
I'm trying to set a string in the main class, and send it to my name class, and then use it later in another method in the main class. This is what I have.
I am fairly new to coding and I just started in a class this year. Anything helps.
I'm just trying to make a simple adventure game.
Thanks in advance!
class Player
{
public string playerName { get; set; }
}
class MainClass
{
public static void nameSelection ()
{
Player player = new Player ();
Console.Clear ();
Console.WriteLine ("Let's start off simple adventurer, what is your name?");
player.playerName = Console.ReadLine ();
Console.Clear ();
Console.WriteLine ("Are you sure {0} is your name? Type 1 for yes and 2 for no", player.playerName);
string confirm = Console.ReadLine ();
if (confirm == "1") {
Console.Clear ();
Console.WriteLine ("Okay {0}, here we go!", player.playerName);
Console.ReadLine ();
}
else if (confirm == "2") {
Console.Clear();
nameSelection ();
}
else
{
Console.Clear ();
nameSelection ();
}
}
public static void classselsction()
{
Console.ReadLine ();
Console.WriteLine ("As you get to the end of the hallway you see a shadow.");
Console.ReadLine ();
Console.WriteLine("Hello {0}, I see you have managed to escape from your cell. " +
"You have proven yourself quite worthey.", player.playerName);
Console.ReadLine ();
}
}
Upvotes: 0
Views: 81
Reputation:
As an alternative to David's suggestion, you might consider making your Player class instance a member of your MainClass. Something like this:
class MainClass
{
static Player player = new Player ();
public static void nameSelection ()
{
// Set player.playerName here
...
}
public static void classselsction ()
{
// Use player.playerName here.
...
}
}
And I agree with his "unrelated" comments. Recursion can be a great tool but it's not needed here. KISS.
Upvotes: 2
Reputation: 218847
So the method nameSelection()
internally creates a variable, and wants to supply that variable to the method classselsction()
when it calls it? Just add it as a method argument:
public static void classselsction(Player player)
{
// the code you already have
}
Then when you call that method you would provide it with the object you created:
classselsction(player);
(Note that you're not currently calling the method at all. But from the description it sounds like you plan to do so?)
Unrelated: You may want to re-think the recursive structure you have going on in nameSelection()
. When you want to re-start the logic based on user input, consider a loop instead of recursion. What you're doing isn't really a recursive thing, you're just re-asking the user for input until a condition is met which is more of a loop. This recursion is going to cause unnecessary confusion with the state of your player
variable otherwise, which is local to any given invocation of the method.
Based on the names of the methods, you probably don't want them calling each other either. I imagine there should be some higher-level method which calls each of them in turn as the input is needed. Though discussions about the overall structure of what you're building can and would quickly exceed the scope of this question.
Basically, as a word of advice... Never try to get clever with your code. Simple things are better than complex things.
Upvotes: 2