Error in C# function to increment variable

Asked

Viewed 1,123 times

6

Guys, I’m new and I have some "basic questions".

I’m making a score, because there are many players I want to create the function to decrease/ increment.

Incremental function

    public void incremento(int pt, int mago)
    {
        pt++;
        switch (mago) { 
            case 1:
                labelM1.Text = String.Format("{0} Pontos", pt);
                break;

        }
     }

Event button +(increment)

 private void M1mais_Click(object sender, EventArgs e)
    {
        incremento(pt1, mago1);
    }

But the label where the current total of points should appear only increases the first time.

What’s wrong? That’s the best way?

3 answers

9


Its increment function performs the increment but the value of the variable is lost. Ideally its increment function would change the value of the variable pt.

It can be done like this:

public void incremento(ref int pt, int mago)
{
    pt++;
    switch (mago) { 
        case 1:
            labelM1.Text = String.Format("{0} Pontos", pt);
            break;
        default:
            break;
    }
}
  • As the value is read within the method, it should be ref instead of out.

  • @Miguelangelo Thanks! Already corrected.

3

The main problem is that you are not making use of Object Orientation. But let’s go in parts.

Solution 1

You are passing a Type variable by value as parameter. Are you wearing a int. A int is a type of value. When you pass a type of value as an argument of a function, it receives only a COPY of its variable, not its variable. That is: it arranges the value in the function but you do not see the change outside of it.

To resolve this, you must use the ref modifier, as quoted by @Cigano there. This will cause the parameter to be passed by reference. Your method will act on your variable (same memory address) and no longer on a copy of it. But it has to be both in the definition of the method and in the call of the same.

You don’t usually notice this error with variables of the Reference Type - briefly everything that is not Value Type (all types of Int and Float, decimal and structs) - because in practice it is as if the object being passed as reference.

Solution 2

Refactor. I would say that, although the code compiles and works, the general logic is essentially mistaken, from the point of view of object orientation.

In my opinion you should be passing an object of type Mago as argument and not two int. It may sound silly now, but it’s better to start the right way.

0

The problem is that as the parameter pt becomes a copy of pt1 the moment you call the method incremento, only the value of pt (within the method) is modified, while pt1 (outside) remains the same.

An alternative would be to separate the values of your label into two: one after (corresponding to the text "Points") and the other before the blank (' ') space (corresponding to the number), using the method Split.

private void M1mais_Click(object sender, EventArgs e)
{
    pt1 = int.Parse(labelM1.Text.Split(' ')[0]); // [0] se refere ao primeiro índice do array de strings gerado, ou seja, ao número.
    incremento(pt1, mago1);
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.