Hello community, I'm trying to create a form with the sort methods (Bubblesort and shell). The Sort algorithms work fine but my problem is that I want to specify the length of the array and add the values. For this I have 2 textbox and a button (insert), the first one is for the length and the second textbox is where you type the value. When you push the insert button the value in textbox2 will be inserted in the array. I can insert values into an array but where I'm having trouble is in the button. How can I call the button1_Click function in another function(void insert) and put in in a for?
Thank you.

Alright, I'm using ArrayList and now I can add values to my array, the problem is with the sort algorithms.

if (vector[j] > vector[j + 1])
                        {
                            int aux = vector[j];
                            vector[j] = vector[j + 1];
                            vector[j + 1] = aux;
                        }
                    }

Error in the if: operator '>' cannot be applied to operand to type 'object' and 'object'

Error in int aux = vector[j];: cannot implicit convert type 'object' to 'int'

Can't find the solution.

Unless you are studying how sort algorithms work, you are making your life very complicated. Use a generic List instead of an ArrayList.

Edited 6 Years Ago by ddanbe: n/a

Agreed...ArrayList stores objects, if you want to use an ArrayList you will have to unbox them before using them:

//assuming you only store integer values in the arraylist
if ((int)vector[j] > (int)vector[j + 1])
{
    int aux = (int)vector[j];

alternatively, as ddanbe suggested, if you only store the same type of values in the list then use a typed list. It is more efficient as it only uses as much memory as your datatype needs and avoids boxing and unboxing your objects:

List<int> vector = new List<int>;
vector.Add(1);
vector.Add(2);

if (vector[j] > vector[j + 1])
{
    int aux = vector[j];

Also thought i'd check - when you are using vector[j + 1], i assume that j is always less than vector.Length - 1?

Edited 6 Years Ago by Geekitygeek: n/a

Comments
Well explained! :)
This article has been dead for over six months. Start a new discussion instead.