Getting error using random generator

I am getting this error when tring to use a random generator:

Program.cs(23,31): error CS0029: Cannot implicitly convert type 'int' to 'System.Random'

Here is my code for that part:


public class Character
    {
        Random randomMoney = new Random();
 
        public Random RandomMoney
        {
            get
            {
                return randomMoney;
            }
            set
            {
                randomMoney = randomMoney.Next(100);
            }
        }
    }

Open in new window

jschmuffAsked:
Who is Participating?
 
galar71Connect With a Mentor Commented:
You're getting the error message because randomMoney.Next returns a value of the type "int", and you are trying to assign it back to your randomMoney variable which is an object of type Random.

What exactly are you trying to achieve?

You might consider changing your class to the following:

    public class Character
    {
        private Random randomMoney = new Random();
 
        public int RandomMoney
        {
            get
            {
                return randomMoney.Next(100);
            }
        }
    }

Open in new window

0
 
ShazbotOKCommented:
   public class Character
    {
        Random randomMoney = new Random();

        public Random RandomMoney
        {
            get
            {
                return randomMoney;
            }
            set
            {
                randomMoney = value(100);
            }
        }
    }
0
 
jschmuffAuthor Commented:
galar71>>
I just want a random number generated I just named it money it could be anything really... what you showed me there doesnt work still get the error:

Program.cs(19,24): error CS0029: Cannot implicitly convert type 'int' to 'System.Random'
0
 
jschmuffAuthor Commented:
No wait I missed the int instead of the Random I think it works now
0
 
galar71Commented:
Yeah, I just changed it that way... Otherwise (if you still want to return an object of type Random) you could for instance change the get method to be either

return new Random();

...or...

return new Random(randomMoney.Next(100));
0
All Courses

From novice to tech pro — start learning today.