views:

121

answers:

5

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.

+1  A: 

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 (i.e. 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).

Tim
Thanks Tim (for being so patient), and for answering this question and being so helpful. The other question was answered well too! There is a reason my code is so jumbled. I had to recently add a lot to an already working project of mine, and it started to get messy.
classicrock985
So, say I wanted to refer to this: public static Bet EmptyBet { get { return new Bet() { amount = 0, fish = 0, better = this }; } } How would I refer to that function in another class? I can't do "Bet.EmptyBet();" because you can't just call functions like that. So how could I "call" that function?
classicrock985
See my latest edit. It sounds like you are trying to have a static property or method that returns an empty but initialized instance of a Bet. Correct?
Tim
Yes that is correct!
classicrock985
The class is called the "Bet" class, and better is the "person" that makes the bet. if that makes any sense?
classicrock985
I added another code example. This shows an instantiable Bet class with a couple of instance members, and a single static member that will return an initialized instance. Remember, there is only one copy of a static property or function, and it will behave accordingly. The example I gave is also similar (though not the same) to the a singleton pattern which only allows one instance of an object to exist.
Tim
Well, part of what I need to do for this particular "game" is declare all of the instance variables to be public properties defined with getand set accessor methods. So would your method be doing this? Or am I thinking of something different?
classicrock985
If that's the use case, then it doesn't sound like you need anything to be static, just public. I will post a simple example of that.
Tim
Oh ok, thanks! I actually understand why that works! Thanks! I definitely would not have thought of that. I'm also getting a new error:
classicrock985
public void collect(int pool, int sharkPool) { return cash += betClass.payout(pool, sharkPool); }"Error 8 Since 'project1.Guy.collect(int, int)' returns void, a return keyword must not be followed by an object expression Lab 3\Guy.cs 90 4 lab3"
classicrock985
That one is simple; if you have a method marked as returning void, it can't return a value. Looks like it should return a numeric type instead.
Tim
Oh, that makes sense. Well "int" probably won't work since it's calculating other int's right? In other words, even though I changed it, I'm still getting the same error.
classicrock985
an Int32 can hold +- 2,147,483,648 so unless you need more than that (or you need to use decimal places), an integer should be fine. Why don't you post an updated version of your complete code/errors?
Tim
I will. This is going to sound very stupid, but how do I edit my original post?
classicrock985
You may not have enough rep to edit it...otherwise there should be a button right underneath your original post. Alternatively, you could open a new thread, as long as your question(s) are clearly stated.
Tim
No, I found it. Thanks!
classicrock985
It has been edited.
classicrock985
I've posted two more updates for you. I'm out for now, have to get some real work done. Good luck.
Tim
Hey Tim, thanks so much for your help. I decided to do something a tiny bit of a different way. I did end up using get and set accessor methods, and now I understand them finally. However, with this new way, I am using a while loop with nested foor loops, and it needs to call to another function, so I am getting a little stuck. Any help is greatly appreciated!
classicrock985
Why don't you send me your complete project? My email address is in my profile. At this point, it will be helpful to see everything.
Tim
A: 

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

Bet bet = new Bet();//first
bet.Desciption; //after
pinichi
does it matter if these are public or private? or is that not even something to worry about at this point?
classicrock985
You mean Desciption ? certainly you must have enought Access Modifiers here, but that not make your `Error 9` . Desciption supposed to be a property access via get,set or a public member of Bet (supposed a class )
pinichi
A: 

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

Andy White
thanks! this helped correct that error! (and many others)
classicrock985
A: 

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)

Andrew Cooper
Thanks! however this added one new error.
classicrock985
A: 
MAKKAM