2

I was just wondering if you could quickly answer a question for me. I'm doing a text adventure game, and I'm trying to have the player set their name in the main area, and store that to a variable and use it throughout other methods.

public static void Main(string[] args)
{
    //intro

    System.Console.WriteLine("Welcome to TextAdventure!");
    System.Console.WriteLine("This is an entry level program made by JussaPeak");
    System.Console.WriteLine("commands will be noted in the text by (). enter an applicable choice.");
    System.Console.WriteLine("  ");
    System.Console.WriteLine("  ");
    System.Console.WriteLine("Please enter your name:");
    string name = Convert.ToString(Console.ReadLine());
    System.Console.WriteLine("  ");
    System.Console.WriteLine("Hello, {0}. You are about to embark on a lackluster journey of my first text adventure. i hope you enjoy!", name);
    System.Console.WriteLine("  ");
    System.Console.WriteLine("  ");
    System.Console.WriteLine("  ");
    System.Console.WriteLine("  ");
    System.Console.WriteLine("  ");

    StartingArea();
}
public static void GypsyConvo2()
{
    Console.WriteLine("You decide to stay and hear her out");
    Console.ReadKey();
    Console.WriteLine("{0}: Who is this person?", name);
    Console.ReadKey();
    Console.WriteLine("Gypsy: He is the shapeshifting king from the mountain in the west. His land is untouched by ours.");
    Console.WriteLine("'I believe he is plotting to take our land...'");
    Console.ReadKey();
    Console.WriteLine(" ");
    Console.WriteLine("{0}: and what am i to do about that?", name);
    Console.ReadKey();
    Console.WriteLine("Gypsy: You must warn the queen. i've found an old sewer maintainence that can lead near the entrance to the castle itself. You'll have to find your own way from there.");


}

So how would i make it so the variable that's being assigned a value from ReadLine in main, usable in other methods without it throwing me errors?

Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
  • _"without it throwing me errors"_ -- what errors? What have you tried? What error are you having trouble figuring out? Certainly, in your static class you can declare static fields that can be shared by all the different methods in your class. One method can set the field, any other method can read the field. It's not clear at all what your _question_ is though (as evidenced by the FGITW answers falling over themselves trying to guess what you mean). – Peter Duniho Jul 04 '16 at 06:04
  • i declare the variable in Main. it takes the value from ReadLine. im trying to use it in other methods, yet it keeps giving me a number of errors. errors such as: an object reference is required for the non-static field, method, or property for program.name. and when its not declared outside main, it just tells me it doesnt exist in the current context. ive tried changing the return types of my different methods, adding the variable as an argument, and it just keeps throwing me those errors. – Deron EZ Lauver Jul 04 '16 at 06:13
  • my appologies. thank you for letting me know to search the specific error message, rather than the general problem. – Deron EZ Lauver Jul 04 '16 at 06:28
  • If you agree that your question is a duplicate of the proposed one, you should go ahead and vote to close your own question as a duplicate, using that other question as the target. That will ensure that the related questions are more easily found as related to each other. – Peter Duniho Jul 04 '16 at 18:36
  • If answered questions are used as a form of reference, the other one should be deleted. it doesnt actually ask a question, it just says "whats my problem here.", and it turned out to be the same as mine. why would we not keep/prioritize the one that clearly outlines the problem, and gets an answer? im genuinely asking. – Deron EZ Lauver Jul 05 '16 at 14:00
  • _"What's my problem here?"_ is in fact a question. More important, the other question has historical significance, as well as does provides a good [mcve] that reliably reproduces the error message that is clearly described in the question. Your question does none of these things, and is especially poor in light of the fact that it demonstrates a specific _lack_ of attempt to research the error message you were receiving. I.e. there was never any reason for your question to be posted in the first place. – Peter Duniho Jul 05 '16 at 18:03
  • That said, I understand some people get emotionally attached to their posts, even when the posts are evidence of their lack of effort. You're under no obligation to vote-to-close or delete the post; it's just the responsible thing to do, given that your question doesn't provide any searchable context that would be likely to be found by future people with a similar question in need of a similar answer. Anyone with the same problem who actually bothered to search for the answer would find the originally-answered of six years ago. – Peter Duniho Jul 05 '16 at 18:03
  • Fair enough. thank you for the clarification – Deron EZ Lauver Jul 05 '16 at 20:54
  • I have voted to delete the post. i apologize for the misuse. – Deron EZ Lauver Jul 05 '16 at 20:56

2 Answers2

4

If you want to pass the variable to the method, then you need to change the signature of the method to accept an argument:

public static void GypsyConvo2(string name)
{

}

and when you invoke the method, you pass the name like this:

GypsyConvo2(name);

Usage inside the method GypsyConvo2:

Console.WriteLine("{0}: Who is this person?", name);

Form C# Specs:

1.6.6.1 Parameters

Parameters are used to pass values or variable references to methods. The parameters of a method get their actual values from the arguments that are specified when the method is invoked. There are four kinds of parameters: value parameters, reference parameters, output parameters, and parameter arrays.

Or if you don't want to pass it around:

static string name;
static static void Main(string[] args)
{
    //intro

    System.Console.WriteLine("Welcome to TextAdventure!");
    System.Console.WriteLine("This is an entry level program made by JussaPeak");
    System.Console.WriteLine("commands will be noted in the text by (). enter an applicable choice.");
    System.Console.WriteLine("  ");
    System.Console.WriteLine("  ");
    System.Console.WriteLine("Please enter your name:");
    name = Convert.ToString(Console.ReadLine());
    System.Console.WriteLine("  ");
    System.Console.WriteLine("Hello, {0}. You are about to embark on a lackluster journey of my first text adventure. i hope you enjoy!", name);
    System.Console.WriteLine("  ");
    System.Console.WriteLine("  ");
    System.Console.WriteLine("  ");
    System.Console.WriteLine("  ");
    System.Console.WriteLine("  ");

    StartingArea();
}

and keep methods like GypsyConvo2 the same.

Zein Makki
  • 29,485
  • 6
  • 52
  • 63
  • so to use it in a string, would it be something like: console.writeline("{0}: im saying something", GypsoConvo2(name)); with it passed at the declaration of the method? – Deron EZ Lauver Jul 04 '16 at 05:57
  • @FirstStep not a big deal to down-vote the entire answer ! – Zein Makki Jul 04 '16 at 06:02
  • @DeronEZLauver To use it, you just use `name`. Like this: `console.writeline("{0}: im saying something", name);` – Zein Makki Jul 04 '16 at 06:09
  • okay, when i give it to GypsyConvo2 as an argument, when i call GypsyConvo2 now, it says: "There is no argument given that corresponds to the required formal parameter 'name' of 'Program.GypsyConvo2(string)'" ???? – Deron EZ Lauver Jul 04 '16 at 06:16
  • @DeronEZLauver you need to pass a string for the method. You can't call it like this `GypsyConvo2()`, you need to call it like this `GypsyConvo2("i am a string")` or `GypsyConvo2(name);` as stated in the answer. – Zein Makki Jul 04 '16 at 06:17
  • OHHHHHH I TOTALLY GET IT NOW THANK YOU THANK YOU THANK YOU – Deron EZ Lauver Jul 04 '16 at 06:19
3

Instead of declaring name variable in Main, declare a private variable in class member level (out side main). This is how you can share value of variable between methods.

private string Name;
public static void Main(string[] args)
{
    System.Console.WriteLine("Welcome to TextAdventure!");
        System.Console.WriteLine("This is an entry level program made by JussaPeak");
        System.Console.WriteLine("commands will be noted in the text by (). enter an applicable choice.");
        System.Console.WriteLine("  ");
        System.Console.WriteLine("  ");
        System.Console.WriteLine("Please enter your name:");
        name = Convert.ToString(Console.ReadLine());
        System.Console.WriteLine("  ");
        System.Console.WriteLine("Hello, {0}. You are about to embark on a lackluster journey of my first text adventure. i hope you enjoy!", name);
        System.Console.WriteLine("  ");
        System.Console.WriteLine("  ");
        System.Console.WriteLine("  ");
        System.Console.WriteLine("  ");
        System.Console.WriteLine("  ");

        StartingArea();
}

Other approach would be using parameters in methods but the variable name you have is Name which sounds it is a class member and this does not fits here. But if you feel it is not a class level member or you have other situation where you feel it is not class level then you can declare a parameter in GypsyConvo2()

public static void GypsyConvo2(string name)
{
     //your code goes here
}
Adil
  • 146,340
  • 25
  • 209
  • 204