user2971920
user2971920

Reputation: 27

random numbers to array

I'm writing a program , that create a random numbers and moves to an array. This is my class called randomize:

class Randomize
{
        public int[] _array;
        public int[] Array{ get { return _array; } set { _array= value; } }
        public Randomize(int[] array)
        {
            Array= array;
        }

        public int _min;
        public int Min
        {
            get { return _min; }
            set { _min = value; }
        }
        public int _max;
        public int Max { get { return _max; } set { _max = value; } }

        public Randomize(int min, int max)
        {
            Min = min;
            Max = max;
        }
        public override string ToString()
        {
            return string.Format(Max.ToString(), Min.ToString());
        }
        public override string ToString()
        {
            return string.Format(Array.ToString());
        }

Min and Max is MinValue and MaxValue.

And now my form:

 private void button1_Click(object sender, EventArgs e)
        {
            Randomize min = new Randomize(0, 100);
            Random rand= new Random(); // randomize
            Randomize[] array= new Randomize[10]; 
            for (int i = 0; i < array.Length; i++)
            {
                array[i] = rand.Next(0,100); //draw in loop
            }
            textBox1.Clear(); 
            for (int i = 0; i < array.Length; i++)
            {


            textBox1.Text = textBox1.Text + " " + array[i].ToString(); //show in textbox              
        }    
    }

And my question is how can I request my array and random numbers to my button1.

Now i have error 'cannot implicitly convert type to int' in first FOR loop.

Thanks and regards :)

Upvotes: 0

Views: 5508

Answers (3)

John Alexiou
John Alexiou

Reputation: 29244

Wow there are some things that are problematic here. Your class should own the data, and handle its generation and display. No operations should be in the button event, other than instructing your class to display the data. Also you should have no magic numbers like 10 or 100 in the class, unless they are declared and described as a const member.

As an example look at the code below and see if you have figure out how it is different from your code (in terms of where and what is done).

public class RandomArray 
{
    /// <summary>
    /// create a single random number generator
    /// </summary>
    static readonly Random generator = new Random();
    /// <summary>
    /// here are the random numbers stored
    /// </summary>
    int[] array;
    /// <summary>
    /// store the min, max used to generate the data
    /// </summary>
    readonly int min, max;
    /// <summary>
    /// Constructor only needs how the value limits
    /// </summary>
    /// <param name="min">The minimum value (typical 0)</param>
    /// <param name="max">The maximum value (example 100)</param>
    public RandomArray(int min, int max)
    {
        this.min=min;
        this.max=max;
        this.array=new int[0];
    }
    /// <summary>
    /// Fills the array with random numbers
    /// </summary>
    /// <param name="count">The number of data to generate</param>
    public void Fill(int count)
    {
        this.array=new int[count];
        // fill array with random integers
        for (int i=0; i<array.Length; i++)
        {
            array[i]=generator.Next(min, max);
        }
    }        
    /// <summary>
    /// Copy constructor if needed (optional)
    /// </summary>
    /// <param name="other">A RandomArray to copy the data from</param> 
    public RandomArray(RandomArray other)
    {
        this.min=other.min;
        this.max=other.max;
        this.array=(int[])other.array.Clone();
    }
    /// <summary>
    /// Provide the data
    /// </summary>

    public int[] Array { get { return array; } }
    /// <summary>
    /// Provide the limits used
    /// </summary>

    public int Min { get { return min; } }
    public int Max { get { return max; } }
    /// <summary>
    /// Creates a comma separated list of numbers like <c>[45,32,64,..]</c>
    /// </summary>
    public string ToStringList()
    {
        string[] parts=new string[array.Length];
        for (int i=0; i<parts.Length; i++)
        {
            parts[i]=array[i].ToString();
        }
        return "["+string.Join(",", parts)+"]";
    }
    /// <summary>
    /// Shows only the limits used
    /// </summary>
    public override string ToString()
    {
        return string.Format("RandomArray({0},{1})", min, max);
    }
}

    // Click Event
    private void button1_Click(object sender, EventArgs e)
    {
        RandomArray random_array=new RandomArray(0, 100);
        random_array.Fill(10);
        textBox1.Text=random_array.ToStringList();
    }

Upvotes: 0

Napivo
Napivo

Reputation: 593

 Randomize[] array= new Randomize[10]; 

Should have been

 int[] array = new int[10];

Upvotes: 1

Nikhil Agrawal
Nikhil Agrawal

Reputation: 48558

Problem

Error is in line

array[i] = rand.Next(0,100);

rand.Next(0,100); gives an integer and you cannot convert from int to Randomize. That what's error is telling.

'cannot implicitly convert type to int'

Solution

You should use an array of integer like this

int[] array= new int[10];

Upvotes: 1

Related Questions