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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

        public Random RandomMoney
        {
            get
            {
                return randomMoney;
            }
            set
            {
                randomMoney = value(100);
            }
        }
    }
0
galar71Commented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.