jeudi 3 octobre 2019

random method in setter method in C#

I am new to C#. I am trying to figure how to use the random method inside a setter. This is how my code looks right now:

public class star_wars_figures
    {
        private string charactor;
        private int bounty;

        public star_wars_figures(string charactor)
        {
            Charactor = charactor;            }

        public string Charactor
        {
            get
            {
                return charactor.ToUpper();
            }
            set
            {
                if (value == "Han Solo" || value == "Leia")
                {
                    charactor = value;
                }
                else charactor = "INCORRECT CHARACTOR!!!!";
            }
        }

        public int Bounty
        {
            get
            {
                return bounty;
            }
            set
            {
                Random rnd = new Random();
                bounty = rnd.Next(1, 10);
                bounty = value;
            }
        }
    }

In my main I insatiate with the following:

star_wars_figures sw1 = new star_wars_figures("Han Solo");
Console.WriteLine($"Character is: {sw1.Charactor}");
Console.WriteLine($"Money: {sw1.Bounty}"); 

I am trying to generate a random int number in the setter method of the bounty attribute. Its just returning 0.

What have I missed out here?

-thanks




Aucun commentaire:

Enregistrer un commentaire