Link to home
Start Free TrialLog in
Avatar of liorb
liorb

asked on

How to place random numbers in labels?

Hello all

Using C#. I've placed on a Form two labels.

The code suppose to generate two random numbers and place each number in the labels (first number to first label, second number to second label)

How do I do that in the code?

Code examples will greatly appreciated !

thank you
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

At its simplest:

    Random r = new Random();
    label1.Text = r.Next().ToString();
    label2.Text = r.Next().ToString();
Avatar of liorb
liorb

ASKER

Good solution but it's not exactly what I need.
I forgot to mention that the random number generation has to be in one method and the number placement must be in another method.
It's important that all the numbers generation and math calculations are in other methods then the one which will obtain them in the labels.
Ok...that's pretty vague and smells ~slightly~ like homework.  =\

Please give more details on exactly how this should work.  It could be done in sooooooo many ways....

Do you need a CLASS to hold the two numbers?  ...and you have methods to tell it to generate the two numbers and Properties to retrieve the values?

Any more constraints?  Like the random numbers must be between X and Y?  Can the two random numbers be the same?  Are you going to call this repeatedly?  If so, is a combination that was generated previously considered valid?

I could go on and on....
Avatar of liorb

ASKER

No homework my school days are long over :)
I'm trying to learn C# on my own!

As I mentioned before in my program I have two labels which contain random number.
The process that I'm aim for works like that:
a) Random number generated  numbers between 3-19
b) I do a simple calculation with that number - (x-10)/2
c) The outcome of that calculation inserted to the first label
d) The same process (a-c) goes for the second label (and the third and fourth ....)
e) The numbers that are now represented in the labels will be used for further use

It will be nice if all the calculations and number generations will be in another class that is accessible all time.

I hope that clears things.

Thanks for the much appreciated help
:)
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of liorb

ASKER

First of all thank for your hard work
I appreciate it a lot :)

I now stating, by your example, to understand how the get & set are implemented to use.

What is the roll of "this" command in the code?
"this" refers to the current instance of an object.  It is CONTEXTUAL as what it refers to is based on the what its current SCOPE is.

In the button1_Click() method, "this" is referring to the FORM itself and the actual instance that is being displayed on the screen:

        private void button1_Click(object sender, EventArgs e)
        {
            rp.GenerateRandomPair();
            this.label1.Text = rp.Number1.ToString() + " --> " + rp.CalculatedValue1.ToString();
            this.label2.Text = rp.Number2.ToString() + " --> " + rp.CalculatedValue2.ToString();
        }

In the RandomPair() class:

        private class RandomPair
        {
             // ...
        }

When "this" appears inside that class it is referring to the current instance of RandomPair.  You can create many instances of a class.  The "this" keyword allows us to refer to the instance we are currently using.  In contrast, if we were OUTSIDE the RandomPair class and we wanted to refer to an instance we would need a variable such as:

    private RandomPair rp = new RandomPair(3, 19);

In this case, "rp" is referring to an instance of RandomPair.

With respect to the "get" and "set" implementations...

Note that the "Number1" and "Number2" properties are READ-ONLY because they only have the "get" implementations.  From OUTSIDE the class you can read these properties but not change them.

The "Min" and "Max" properties are read/write because they have both "get" and "set".  You can change their values from outside the class.  We could put additional code inside the "set" methods to prevent the user from setting a min/max value that is smaller/larger than the other value.  We wouldn't want a "min" of 10 and a "max" of 3 right?  I didn't put in an error checking with respect to that.

Avatar of liorb

ASKER

Mike you are the best

This is more then I asked for.
From you I've learned so much - thanks a lot :)

two more Question:
In the code you gave the value 100 to the _max number
"          private Int32 _min = 0;
            private Int32 _max = 100;   "
Why  this number and not another (why not 20 or any other number)?

Why using 'int32' and not just 'int'?

I do have more questions but I afraid to be nuisance

The 0 and 100 are just arbitrary values I picked.  =)

It doesn't matter much anyway since I made the constructor accept the desired min/max values:

    public RandomPair(Int32 min, Int32 max)

So the 0 and 100 just get replaced when the class is instantiated...

Some great discussion on 'int' vs 'int32':
http://stackoverflow.com/questions/62503/c-int-or-int32-should-i-care
Essentially they are the same.