【问题标题】:Passing variables to different methods in c# [duplicate]在c#中将变量传递给不同的方法[重复]
【发布时间】:2016-07-04 15:02:36
【问题描述】:

我只是想知道您是否可以快速为我回答问题。我正在做一个文字冒险游戏,我试图让玩家在主区域中设置他们的名字,并将其存储到一个变量中并在其他方法中使用它。

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.");


}

那么我将如何使从 main 中的 ReadLine 分配值的变量在其他方法中可用而不会引发错误?

【问题讨论】:

  • "不会给我带来错误" -- 什么错误?你试过什么?您无法弄清楚什么错误?当然,在您的静态类中,您可以声明可以由类中所有不同方法共享的静态字段。一种方法可以设置字段,任何其他方法都可以读取字段。完全不清楚您的 question 是什么(正如 FGITW 的答案在试图猜测您的意思时跌倒自己所证明的那样)。
  • 我在 Main 中声明了变量。它从 ReadLine 获取值。我试图在其他方法中使用它,但它一直给我一些错误。错误,例如:program.name 的非静态字段、方法或属性需要对象引用。当它没有在 main 之外声明时,它只是告诉我它在当前上下文中不存在。我尝试更改不同方法的返回类型,将变量添加为参数,但它只是不断向我抛出这些错误。
  • 我的道歉。感谢您让我知道要搜索特定的错误消息,而不是一般问题。
  • 如果您同意您的问题与提议的问题重复,您应该继续投票以关闭您自己的问题作为重复问题,并使用其他问题作为目标。这将确保更容易找到彼此相关的相关问题。
  • 如果已回答的问题用作参考形式,则应删除其他问题。它实际上并没有问一个问题,它只是说“我的问题是什么。”,结果和我的一样。为什么我们不保留/优先考虑清楚概述问题并得到答案的问题?我真的在问。

标签: c#


【解决方案1】:

如果要将变量传递给方法,则需要更改方法的签名以接受参数:

public static void GypsyConvo2(string name)
{

}

当您调用该方法时,您会像这样传递name

GypsyConvo2(name);

方法内部使用GypsyConvo2:

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

Form C# 规范:

1.6.6.1 参数

参数用于将值或变量引用传递给方法。 方法的参数从参数中获取它们的实际值 调用方法时指定的。有四种 参数:值参数、参考参数、输出参数、 和参数数组。

或者如果你不想传递它:

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();
}

并保持GypsyConvo2 之类的方法不变。

【讨论】:

  • 所以要在字符串中使用它,会不会是这样的:console.writeline("{0}: im say something", GypsoConvo2(name));在方法的声明中传递它?
  • @FirstStep 否决整个答案并不是什么大问题!
  • @DeronEZLauver 要使用它,您只需使用 name。像这样:console.writeline("{0}: im saying something", name);
  • 好的,当我将它作为参数提供给 GypsyConvo2 时,当我现在调用 GypsyConvo2 时,它会说:“没有给定的参数对应于 'Pr​​ogram.GypsyConvo2 所需的形式参数'名称' (字符串)'" ????
  • @DeronEZLauver 您需要为该方法传递一个字符串。你不能这样称呼它GypsyConvo2(),你需要像答案中所说的那样称呼它GypsyConvo2("i am a string")GypsyConvo2(name);
【解决方案2】:

不要在 Main 中声明 name 变量,而是在类成员级别(外部 main)声明一个私有变量。这就是您可以在方法之间共享变量值的方式。

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();
}

其他方法是在方法中使用参数,但您拥有的变量名称是 Name 听起来它是一个类成员,这不适合这里。但是如果你觉得它不是类级别的成员,或者你有其他情况你觉得它不是类级别,那么你可以在 GypsyConvo2() 中声明一个参数

public static void GypsyConvo2(string name)
{
     //your code goes here
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-07
    • 2015-06-25
    • 1970-01-01
    • 2016-04-20
    • 1970-01-01
    相关资源
    最近更新 更多