简体   繁体   中英

Why am I getting these errors in C#?

EDIT:

I got rid of most of my errors. Thanks for your help.

There's something wrong with my while loop (in my Form class), but basically, it tests to see who the winner of the "race" is in my application, and it also starts the application (Shark.Swim). Once it finds out who the winner is, it needs to get to the "payout" method in my Bet Class.

So here's what I have.

WHILE LOOP:

private void raceBtn_Click(object sender, EventArgs e)
    {
        public int[] finishingOrder = new int[4];
        bool sharkFinished = false;
        public int place = 1;
        public int numSharksFinished;

while (numSharksFinished < 4)
        {
            sharkFinished = false;
            for (int i = 0; i < 4; i++)
            {
                if (finishingOrder[i] == -1)
                {
                    if (sharks[fish].Swim();)
                    {
                        finishedOrder[i] = place;
                        sharkFinished = true;
                        numSharksFinished++;
                    }
                }
                if(sharkFinished = true) 
                {
                   place++;
                }
            }
        }

PAYOUT:

public double payout(int pool, int sharkPool)
    {
        for (int j = 0; j < 3; j++)
        {
            Guy[j].cash += Bets[i, j].Amount;
        }

    }

I think my best bet is moving the "payout" method to the main forms class, because there is no instance of the "Bets" Array in my Bet class.

You are using type names as instance variables. For example:

return cash += Bet.payout(pool, sharkPool);

If Bet.payout() isn't a static method, then you need to create an instance of Bet first to call this method.

For example:

Bet b = new Bet();
return cash + b.payout(pool, sharkPool);

This appears to be the problem in a couple of different places.

EDIT: adding a few more suggestions.

Bet EmptyBet { get { return new Bet() { amount = 0, fish = 0, better = this }; }

The problem here is that you are using an instance reference ( this ) inside of a static accessor. Think of a static member as the one and only copy of something. As such, it doesn't know about instances (ie specific, unique occurrences of a type).

I'm not sure what better represents, but you can't assign it to this , given the context.

EDIT #2: Here's a little class I mocked up to help you understand how to create a static member that will return a default bet.

    public class Bet
    {
        #region instance members

        public decimal Amount
        {
            get;
            set;
        }

        public string Description
        {
            get;
            set;
        }

        public void Payout()
        {
            // do something
        }

        #endregion

        #region static members

        public static Bet Empty
        {
            get
            {
                Bet b = new Bet();
                b.Amount = 0M;
                b.Description = "Default Empty Bet";

                return b;
            }
        }

        #endregion
    }

EDIT #3: Declaring instance properties with accessors

//
// This will not compile because members have been defined more than once
public class Bet
{
    // You can declare smart properties with default initializers
    // by not declaring a body for the get/set accessors

    public decimal Amount
    {
        get;
        set;
    }

    public string Description
    {
        get;
        set;
    }

    // OR, you can declare private variables and expose them
    // publicly via get/set accessors. This gives flexibility
    // in internal manipulation (sometimes) but creates more code

    private decimal _amount = 0M;

    public decimal Amount
    {
        get { return _amount; }
        set { _amount = value; }
    }
    private string _description = string.Empty;

    public string Description
    {
        get { return _description; }
        set { _description = value; }
    }
}

EDIT #4:

public void collect(int pool, int sharkPool)
{
    return cash += betClass.payout(pool, sharkPool);
}

Error 4 Cannot implicitly convert type 'double' to 'int'. An explicit conversion exists (are you missing a cast?) Lab 3\\Guy.cs 90 19 lab3

A double is a more precise type than an int (in a general manner of speaking). This means that it can't be implicitly "stuffed" into the memory space allocated for an int.

If you are using a double inside the Guy class, then you need to either change it to an int, or cast it where appropriate, or use doubles all the way through the process. A double allows for a decimal point, which would probably be needed in a real application that managed money (or better yet, the decimal type).

EDIT #5:

private int winningSharkTotal(int Winner)
{
int Result = 0;


for (int i = 0; i<3; i++)
{
    Result += bets[i+(Winner*3)];

}
    return Result;


}

Couple things wrong with this one...first, the casing of your variables is really confusing, because when most c# programmers see an uppercase variable name, it looks like a type, not a method parameter (notice how the SO editor formats it wrong).

Second thing, I don't know what logic you are trying to achieve with this:

bets[i+(Winner*3)]

Whatever i + (winner * 3) equals will be the array index that is used. This is ambiguous at best. Perhaps you were trying to triple the winner's earnings?

Lastly, bet[index] will return a Bet object, not an int. You need something more like bet[index].Amount (or whatever the property you are using is).

You try to access Description of Bet, but Bet is not an instance.

Bet bet = new Bet();//first
bet.Desciption; //after

For error 1, the problem is that you have not declared the type of 'i'. Change it to:

for (int i = 0; i < bets.Length; i++)

In C#/.NET, when you introduce a new variable, you have to declare the type. (Or you can declare the variable as a "var" to let the compiler infer the type, but I wouldn't worry about that at this point, just use int for this.)

In the first two, do you have an object called Bet? It looks to me like Bet here is referring to the class name, rather than an object variable.

The for loop should be for (int i=0; ...)

winner needs a capital W ( Winner )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM