Search code examples
c#classclass-library

Getting always a zero when I execute - c#


So I'm doing something stupid in my class solution because I am expecting a number but I get a zero. The code is as follows:

class Spel
{
    private int _nummer1;
    public int Nummer1
    {
        get { return _nummer1; }
        set { _nummer1 = value; }
    }
    private int _nummer2;
    public int Nummer2
    {
        get { return _nummer2; }
        set { _nummer2 = value; }
    }
    private int _nummer3;
    public int Nummer3
    {
        get { return _nummer3; }
        set { _nummer3 = value; }
    }
    private int _punten;
    public int Punten
    {
        get { return _punten; }
        set { _punten = value; bepaal(); }
    }
    public void bepaal()
    {
        Random rnd = new Random();
        _nummer1 = rnd.Next(1, 4);
        _nummer2 = rnd.Next(1, 4);
        _nummer3 = rnd.Next(1, 4);
        if (_nummer1 == _nummer2 && _nummer2 == _nummer3 && _nummer1 == _nummer3)
        {
            _punten = 10;
        }
        else if (_nummer1 == _nummer2 && _nummer2 == _nummer3)
        {
            _punten = 5;
        }
        else if (_nummer1 == _nummer2 && _nummer1 == _nummer3)
        {
            _punten = 5;
        }
        else if (_nummer2 == _nummer3 && _nummer1 == _nummer3)
        {
            _punten = 5;
        }
    }

I think the fault is in the area where I give the 'nummer' a random number. Here is the code where I call the class with a controller:

public class Controller
{
    private Spel spl = new Spel();
    public int getNummer1()
    {
        return spl.Nummer1;
    }

In the next code I'm using the controller to bring up the class:

private void button1_Click(object sender, EventArgs e)
    {
        teller++;
        label1.Text = spl.getNummer1().ToString(); 
....

I don't know where the fault is so, can someone help me?


Solution

  • There are several issues with this code.

    One of them has to do with the Punten properties, which is calculated and assigned a value at the same time.

    The code below needs revising.

    private int _punten;
    public int Punten
    {
        get { return _punten; }
        set { _punten = value; bepaal(); }  // BUG: assign _punten twice
    }
    public void bepaal()
    {
        Random rnd = new Random();
        _nummer1 = rnd.Next(1, 4);
        _nummer2 = rnd.Next(1, 4);
        _nummer3 = rnd.Next(1, 4);
        if (_nummer1 == _nummer2 && _nummer2 == _nummer3 && _nummer1 == _nummer3)
        {
            _punten = 10;
        }
        else if (_nummer1 == _nummer2 && _nummer2 == _nummer3)
        {
            _punten = 5;
        }
        else if (_nummer1 == _nummer2 && _nummer1 == _nummer3)
        {
            _punten = 5;
        }
        else if (_nummer2 == _nummer3 && _nummer1 == _nummer3)
        {
            _punten = 5;
        }
        // missing else branch, major bug.
    }
    

    I recommend a property with a public getter and private setter instead. Also the values of the properties are all assigned at Bepaal() so there is no need for a public setter to exist.

    class Spel
    {
        static readonly Random rnd = new Random();
    
        public int Nummer1 { get; private set; }
        public int Nummer2 { get; private set; }
        public int Nummer3 { get; private set; }
        public int Punten { get; private set; }
    
        public void Bepaal()
        {
            Nummer1 = rnd.Next(1, 4);
            Nummer2 = rnd.Next(1, 4);
            Nummer3 = rnd.Next(1, 4);
            if (Nummer1 == Nummer2 && Nummer2 == Nummer3 && Nummer1 == Nummer3)
            {
                Punten = 10;
            }
            else if (Nummer1 == Nummer2 && Nummer2 == Nummer3)
            {
                Punten = 5;
            }
            else if (Nummer1 == Nummer2 && Nummer1 == Nummer3)
            {
                Punten = 5;
            }
            else if (Nummer2 == Nummer3 && Nummer1 == Nummer3)
            {
                Punten = 5;
            }
            else
            {
                Punten = 0;    // missing default
            }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Spel spel = new Spel();
            spel.Bepaal();
            Console.WriteLine(spel.Punten);
        }
    }
    

    Also note that the Random variables needs to be initialized only once, and it is a mistake to call new Random() every time a random number is needed. It is common practice to store a Random variable as a static readonly field and to be initialized only once, as seen above.