Reputation: 249
I am building a game where you are going to be able to bet a amount of points.
When you bet I get the value the user types in like this:
Class Xyz.cs
public static void Bet(int betAmount)
{
Console.Write("How many points do you want to bet? ");
int bet = int.Parse(Console.ReadLine());
Console.ReadLine();
}
Now t´when you have made the bet there is a dice that rolls and depending on what the value the dice gets you are going to lose or gain points.
And I try to call the int
bet here:
Class abc.cs
public static void GainPoints()
{
PlayerPoints = PlayerPoints + bet * 5;
}
But bet gets the red underscore and if I hover over it it I get this error:
The name 'bet' does not consist in the current context
The property PlayerPoints
is private.
Thanks in advance!
EDIT:
The two methods is in separate classes.
Upvotes: 0
Views: 63
Reputation: 48415
The reason for the error is that the bet
variable is local to the Bet
function and cannot be accessed by any methods outside of it.
It would make more sense for your Bet
function to return the value of the bet for starters, and rename it to something more sensible:
public static int GetBet(int betAmount)
{
Console.Write("How many points do you want to bet? ");
int bet = int.Parse(Console.ReadLine());
Console.ReadLine();
return bet;
}
Then you can pass the value into your other function when you call it. Change your GainPoints
function like so:
public static void GainPoints(int bet)
{
PlayerPoints = PlayerPoints + bet * 5;
}
Then you call the functions something like this, for example:
int bet = Class1.GetBet(betAmount);
Class2.GainPoints(bet);
Now, any experience developer would probably recognise that the design here isn't the best. It would probably be better to have some sort of Bet
class that tracks information on the bet and passes it around as needed. However, it would be far too broad to go into those kind of details here.
Upvotes: 2