Click here to Skip to main content
14,269,076 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hello

I am trying to write this program that will list user input from 5 textbox and display them in a orderd and reverse order list in two lables

the way I got it now shows it but I like to have it accending like textbox1 textboxt2 for the orderd and reverse textbox2 , textbox1
And have it allign to left in that order in the lable

Hear is the code I have now with two I don't know error I like to fix also

public form1()
{
    InitializeComponent();
}


public partial class Form1 : Form
{
    Array arrayList();
    //Error	1	'Form1.arrayList()' must declare a body because it is not marked abstract, extern, or partial	
}      

private void btndisplay_Click(object sender, EventArgs e)
{
    arrayList list = new arrayList();
    //Error	2	The type or namespace name 'arrayList' could not be found (are you missing a using directive or an assembly reference?)	


    list.Add = txtitem1.Text;
    list.Add = txtitem2.Text;
    list.Add = txtitem3.Text;
    list.Add = txtitem4.Text;
    list.add = txtitem5.Text;
    //
    // Sort the ArrayList.
    //
    list.Sort.accend ();
    //
    // Display the ArrayList elements.
    //
    lblOrder.Text = list();

        {

        }
    //
    // Reverse the ArrayList.
    //
    list.Reverse.decend();
    //
    // Display the ArrayList elements again.
    //
    foreach (string data in list)
        list.reverse = lblReverse.Text();

    return (list);

}

private void btnClear_Click(object sender, EventArgs e)
{
    lblOrder.Text = ("");
    lblReverse.Text = ("");
    txtitem1.Text = ("");
    txtitem2.Text = ("");
    txtitem3.Text = ("");
    txtitem4.Text = ("");
    txtitem5.Text = ("");
}

private void btnExit_Click(object sender, EventArgs e)
{
    this.Close();
}
Posted
Updated 1-Apr-12 12:12pm
v3
Rate this:
Please Sign up or sign in to vote.

Solution 1

public partial class Form1 : Form
{
    ArrayList arrayList;

}      
 
private void btndisplay_Click(object sender, EventArgs e)
{
    ArrayList list = new ArrayList();


Although arrayList is never used according to your code.

I take it from your comment that you want to know why as well at 'what'.

Your first error:
Array arrayList();

The () means that you are trying to call the constructor for a variable declaration. No such thing exists. Variable declarations do not use (). What you really wanted is:
Array arrayList;

Which would declare a variable arrayList of type Array. But since you did not assign the variable any value it will have its default value of null. AND since arrayList is declared within the scope of the form constructor, that is the only place were you could use it.

Your second error comes at the line:
arrayList list = new arrayList();

Which means that you are declaring a variable list of type arrayList and instantiating an instance of that class. Well the problem is that you have not defined what class 'arrayList' is. So you get the type or namespace error. 'arrayList' in your original code would be a variable and could not be instantiated as a new instance of a class. And would not be visible to this method since that variable is scoped to the form constructor.

To actually fix your code depends on what it is that you really want to do. Do you want an Array or an ArrayList? Do you really want a class level or a function level variable? I do not see where you are using the list outside of the function so I would not imagine that you need a class level variable.

The third problem that has not been mentioned is:
return (list);

as your function is declared to return void then trying to return a list from it, won't work.
   
v2
Comments
Wes Aday 1-Apr-12 20:29pm
   
Brilliant. Downvoted for giving the OP the code that will correct both of his errors. That's just great. No wonder people stop answering questions. And like the greats before me, I'm done also.
leetank 3-Apr-12 13:45pm
   
Wes I'm sorry if giving me the answer got you down voted i think its Wong cause i greatly appreciated your response
Wes Aday 3-Apr-12 20:19pm
   
You're welcome. Hopefully it helps.
leetank 3-Apr-12 13:44pm
   
what do you mean wes?
Wes Aday 3-Apr-12 20:18pm
   
See updated.
ProEnggSoft 5-Apr-12 0:15am
   
Nice explanation of cause of errors. +5.
Wes Aday 5-Apr-12 6:46am
   
Thanks
member60 5-Apr-12 1:39am
   
my 5!
Wes Aday 5-Apr-12 6:46am
   
Thanks
Rate this:
Please Sign up or sign in to vote.

Solution 2

The reasons for errors were nicely explained by Wes Aday in Solution 1.
The following code can be used to display the text of TextBoxes in Order and in Reverse order.
private void btndisplay_Click(object sender, EventArgs e)
{
    List<string> textBoxesContent =new List<string>{
        txtitem1.Text,txtitem2.Text,txtitem3.Text,txtitem4.Text,txtitem5.Text};
    textBoxesContent.Sort();
    //Using LINQ
    lblOrder.Text = textBoxesContent.Aggregate( (s1,s2) => s1 + ", " + s2 );
    //Using ForEach
    lblOrder.Text = ConcatStrings(textBoxesContent,", ");
    textBoxesContent.Sort((s1,s2)=>s2.CompareTo(s1));
    //Using LINQ
    lblReverse.Text = textBoxesContent.Aggregate ((s1,s2) => s1 + ", " + s2);
    //Using ForEach
    lblReverse.Text = ConcatStrings(textBoxesContent,", ");
}
//To concatenate strings of the List using ForEach loop
public static string ConcatStrings(IEnumerable<string> stringList, string separator){
    string concatenatedString = string.Empty;
    foreach (string element in stringList)
    {
    	if(!string.IsNullOrEmpty(concatenatedString))
    		concatenatedString += separator;
    	concatenatedString += element;
    }
    return concatenatedString;
}
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100