Sarp Kaya
Sarp Kaya

Reputation: 3784

usage of global variable on C#

So I don't know the terminology of C#. What I am trying to do is I have 2 static voids

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

My problem is I want to use moneyamount in InsertCoin void. When I assign double moneyamount just right after class It gives an error. I cannot return moneyamount as it is static. and I have to use static as i need to recall it. So what can I do in this situation?

Upvotes: 2

Views: 1346

Answers (6)

ABH
ABH

Reputation: 3439

You can create the static property of 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);
}

Or you can make parameter to 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);
}

On another note;

I cannot return moneyamount as it is static

Why can't you do that?

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

Upvotes: 0

KMC
KMC

Reputation: 20046

Declare your moneyamount in the class, not in the class method, and make it static too. That way static methods in the same class can access your static variable moneyamount.

Declaring the method as static means the method is part of a class, and can be access directly from another class. Without static, that become part of the object instance, and can be accessed through an object. Hence a static method access a static variable:

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

Upvotes: 0

Brendan Vogt
Brendan Vogt

Reputation: 26018

You have 2 choices here. You can either define moneyamount as a global variable outside of your 2 methods and have it available for both methods, or you declare it like you did and just pass it to the InsertCoin method.

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

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

I hope this helps. Shout if you need more help.

Upvotes: 1

tobias86
tobias86

Reputation: 5029

You have two options

Option 1: Pass moneyAmount as a parameter to InsertCoin(), i.e.

static void InsertCoin(double moneyAmount)...

then call it from SelectProduct as

InsertCoin(moneyAmount);

Option 2: Simple declare your global variable as static, i.e.

static double moneyAmount = 0;

static void SelectProduct()...

static void InsertCoin()...

Upvotes: 2

Krishna
Krishna

Reputation: 2481

you can pass parameters to methods. Please google for "c# functions syntax"

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

Upvotes: 0

PhatHV
PhatHV

Reputation: 8141

Maybe make your moneyamount as static too.

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

Upvotes: 2

Related Questions