C#原始排序列表和原始反向列表帮助 [英] C# Original orderd list and Original reverse list help

查看:100
本文介绍了C#原始排序列表和原始反向列表帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

我正在尝试编写此程序,该程序将列出5个文本框中的用户输入,并以两个标签的有序和反向顺序列表显示它们.

现在,它的显示方式已显示出来,但我想让它像textbox1 textboxt2一样有序和反向textbox2,textbox1的添加
并按顺序将所有标签留在标签中

听到的是我现在有两个的代码,我不知道错误,我也想修复

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();
}

推荐答案

public partial class Form1 : Form
{
    ArrayList arrayList;

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



尽管根据您的代码从未使用过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;


它将声明一个Array类型的变量arrayList.但是,由于您没有为变量分配任何值,因此其默认值为null. AND 因为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();


这意味着您要声明一个arrayList类型的变量列表,并实例化该类的实例.问题是您尚未定义"arrayList"是什么类.这样您会得到类型或名称空间错误.您原始代码中的"arrayList"将是一个变量,无法实例化为类的新实例.由于该变量的作用域为表单构造函数,因此该方法将不可见.

实际修复代码取决于您真正想要做什么.您是否要使用Array或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);



错误的原因已由 Wes Aday 很好地解释了,因为您的函数被声明为返回void,然后尝试从中返回列表,将无法正常工作. >在解决方案1中.
以下代码可用于按顺序和反向显示TextBoxes text.
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;
}


这篇关于C#原始排序列表和原始反向列表帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆