Click here to Skip to main content
15,887,585 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
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

C#
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

C#
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:
C#
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:
C#
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:
C#
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:
C#
return (list);

as your function is declared to return void then trying to return a list from it, won't work.
 
Share this answer
 
v2
Comments
[no name] 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
[no name] 3-Apr-12 20:19pm    
You're welcome. Hopefully it helps.
leetank 3-Apr-12 13:44pm    
what do you mean wes?
[no name] 3-Apr-12 20:18pm    
See updated.
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.
C#
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;
}
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900