0

I want to dispaly my sort array in textBox after click the button. I have two classes and one interface. First Class generate random numbers and show in textbox1, second class make bubble sort and I can't display sort array on texbox.

First class:

public class RandomArray     
{
    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);
    }
<!-- language: none-->

This is bubble sort

class BubbleSort :  ISortAlgorithm<int>
{
   public void Sort (int[] array)     
    {
        int temp;
        for (int j = 1; j <= array.Length - 2; j++)
        {
            for (int i = 0; i <= array.Length - 2; i++)
            {
                if (array[i] > array[i + 1])
                {
                    temp = array[i + 1];
                    array[i + 1] = array[i];
                    array[i] = temp;
                }
            }
        }
    }

ISortAlgorithm:

interface ISortAlgorithm<T>
{
    void Sort(T[] array);
}

The last thing is request to textbox:

private void button2_Click(object sender, EventArgs e)
{            
    BubbleSort p = new BubbleSort();
    textBox1.Text = p.ToString();
}

And if I click the button program displays MyProgram.BubbleSort

Do I override bubblesort method ? or what ? I tried almost everything. Thanskd and regards.

6
  • Where are you calling the sorting code? Commented Nov 11, 2013 at 13:35
  • BubbleSort is in different class and i induce in button2 Commented Nov 11, 2013 at 13:42
  • In the button click event you are not calling the sort method. You just instantiated the class. Commented Nov 11, 2013 at 13:46
  • In that context, you are just getting a string representing the name of your BubbleSort class. Commented Nov 11, 2013 at 13:47
  • @user2971920: The important thing to note is that your BubbleSort class contains no state and indeed the Sort method sorts the method in place and doesn't return a new sort. This means a) you should probably make the class/method static to make it clearer that it is not doing anything. b) once you pass in an array to the sort method the original array is sorted so you just need to output that. Commented Nov 11, 2013 at 13:47

2 Answers 2

4

I think you got a little confused there. You want to create a RandomArray object and call its ToString() method, after you sorted the array, of course.

RandomArray r = new RandomArray(1, 100);
r.Fill(50);
BubbleSort b = new BubbleSort();
b.Sort(r.Array);
textBox1.Text = r.ToStringList(); //depending on the requirements
Sign up to request clarification or add additional context in comments.

6 Comments

On other button i have RandomArray RandomArray l= new RandomArray(0, 100); l.Fill(10); textBox1.Text = l.ToStringList();
@user2971920, you need to save a reference to your l object and pass it as an argument to the BubbleSort.Sort() method.
Only problem that should be clarified in the response is that BubbleSort.Sort doesn't take a RandomArray object. You should be able to do b.Sort(r.Array) though I think.
Almost good, Now i have an error in first loop in Sort, int j=1; j<=array.Length - 2 Null Reference Exception, Object reference not set to an instance of na object
@user2971920: That should really be a different question but if it is in that line then it suggests your array is null so check the code around that. Run it in debug mode and put a breakpoint there so you can confirm what is being passed in to your methods, make sure your RandomArray is generating correctly, etc.
|
-1

You need to override the ToString method of the BubbleSort class

A few more things,

1) You need to keep the array within the BubbleSort class to use it they way you are

class BubbleSort :  ISortAlgorithm<int>
{
   private int[] myArray;
   public void Sort (int[] array)     
    {
        myArray = array;
        int temp;
        for (int j = 1; j <= array.Length - 2; j++)
        {
            for (int i = 0; i <= array.Length - 2; i++)
            {
                if (array[i] > array[i + 1])
                {
                    temp = array[i + 1];
                    array[i + 1] = array[i];
                    array[i] = temp;
                }
            }

        }

    }

    public override string ToString()
    {
        StringBuilder sb = new StringBuilder();
        foreach(int i in myArray)
        {
            sb.Append(Convert.ToString(i));
        }
        return sb.ToString();
    }

5 Comments

Override it to say what? The bubblesort class doesn't store the sorted array currently.
I am busy amending my answer
IF you were doing it this way I'd make myArray a new array and sort that rather than sorting the array that you were passed. I personally think though that this isn't the best way since the class name and implementation suggests that it is a Sorting Algorithm and thus shouldn't really be storing results. It should probably return them (which the interface doesn't allow) or sort the array it is passed (which it does). And then use that passed array as the basis for the output.
In addition to @Chris' comment, I would like to say that if I were to modify this class, I would make static. It does not need to store the state of the targeted array.
-1: Storing result inside sorting algorithm is very confusing. After the array is sorted just ask it to print itself, instead of relying on algorithm tracking it. @AndreiV answer is much better and likely what was intended.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.