在C#上使用全局变量

时间:2012-03-16 07:22:16

标签: c# variables global void

所以我不知道C#的术语。 我想做的是我有2个静态空隙

    static void SelectProduct() {
        double moneyamount = 0;

        int selection = int.Parse(Console.ReadLine());
        if (selection == 1) {
            moneyamount = 1.50;       
        }

        else {
            Console.WriteLine("Wrong Selection");
        }

        Console.WriteLine("Your drink costs $" + moneyamount);
        InsertCoin();
    }
    static void InsertCoin() {

        Console.WriteLine("Balance of cost $" + moneyamount);
    }

我的问题是我想在moneyamount中使用InsertCoin void。 当我在课后恰好分配双倍金额时它会给出错误。 我不能退还金额,因为它是静态的。我必须使用静态,因为我需要回忆它。 那么在这种情况下我该怎么办?

6 个答案:

答案 0 :(得分:2)

也许你的钱也可以保持静态。

static double moneyamount = 0;
static void SelectProduct() {
    moneyamount = 0;

    int selection = int.Parse(Console.ReadLine());
    if (selection == 1) {
        moneyamount = 1.50;       
    }

    else {
        Console.WriteLine("Wrong Selection");
    }

    Console.WriteLine("Your drink costs $" + moneyamount);
    InsertCoin();
}
static void InsertCoin() {

    Console.WriteLine("Balance of cost $" + moneyamount);
}

答案 1 :(得分:2)

您有两个选择

选项1:将moneyAmount作为参数传递给InsertCoin(),即

static void InsertCoin(double moneyAmount)...

然后从SelectProduct调用它

InsertCoin(moneyAmount);

选项2:简单地将全局变量声明为静态,即

static double moneyAmount = 0;

static void SelectProduct()...

static void InsertCoin()...

答案 2 :(得分:1)

这里有2个选择。您既可以将moneyamount定义为2种方法之外的全局变量,也可以将它们用于这两种方法,或者将其声明为与之相同,并将其传递给InsertCoin方法。

情景1:

static double moneyamount = 0;

static void SelectProduct()
{
     int selection = int.Parse(Console.ReadLine());

     if (selection == 1)
     {
          moneyamount = 1.50;
     }
     else
     {
          Console.WriteLine("Wrong Selection");
     }

     Console.WriteLine("Your drink costs $" + moneyamount); 
}

static void InsertCoin()
{
     Console.WriteLine("Balance of cost $" + moneyamount);
}

情景2:

static void SelectProduct()
{
     double moneyamount = 0;
     int selection = int.Parse(Console.ReadLine());

     if (selection == 1)
     {
          moneyamount = 1.50;
     }
     else
     {
          Console.WriteLine("Wrong Selection");
     }

     Console.WriteLine("Your drink costs $" + moneyamount);
     InsertCoin(moneyamount);
}

static void InsertCoin(double moneyamount)
{
     Console.WriteLine("Balance of cost $" + moneyamount);
}

我希望这会有所帮助。如果您需要更多帮助,请大声说出来。

答案 3 :(得分:0)

您可以将参数传递给方法。请谷歌搜索“c#functions syntax”

 static void InsertCoin(double moneyAmount) 
 {          
   Console.WriteLine("Balance of cost $" + moneyamount);     
 } 

答案 4 :(得分:0)

在类中声明您的moneyamount,而不是在类方法中,并将其设为static。这样,同一个类中的静态方法可以访问静态变量moneyamount

将方法声明为static表示该方法是类的一部分,可以直接从另一个类访问。没有static,它成为对象实例的一部分,并且可以通过对象访问。因此静态方法访问静态变量:

static string fooString;
static void FooMethod()
{
    fooString = "foo"
}

答案 5 :(得分:0)

您可以创建MoneyAmount

的静态属性
static double MoneyAmount
{get; set;}

static void SelectProduct() 
{
    MoneyAmount= 0;

    int selection = int.Parse(Console.ReadLine());
    if (selection == 1) {
        MoneyAmount = 1.50;       
    }

    else {
        Console.WriteLine("Wrong Selection");
    }

    Console.WriteLine("Your drink costs $" + MoneyAmount);
    InsertCoin();
}

static void InsertCoin() 
{
    Console.WriteLine("Balance of cost $" + MoneyAmount);
}

或者您可以将参数设为InsertCoin()

static void SelectProduct() 
{
    double moneyamount = 0;

    int selection = int.Parse(Console.ReadLine());
    if (selection == 1) {
        moneyamount = 1.50;       
    }

    else {
        Console.WriteLine("Wrong Selection");
    }

    Console.WriteLine("Your drink costs $" + moneyamount);
    InsertCoin();
}

static void InsertCoin(double moneyamount) 
{
    Console.WriteLine("Balance of cost $" + moneyamount);
}

另一方面,

  

我无法返回moneyamount,因为它是静态的

为什么你不能这样做?

static double SelectProduct()
{
...
return moneyamount;
}