Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在c#中列出“global”?_C# - Fatal编程技术网

如何在c#中列出“global”?

如何在c#中列出“global”?,c#,C#,首先,我的大部分编码经验都是通过python进行的,因此我使用了global 我有一个列表,我希望能够搜索列表并找到x长度的所有单词。我的主要问题是不知道如何从事件侦听器内部调用函数 因此,myList是在我的函数functionOne中生成的,然后我想在functionTwo中使用这个列表,最后我想在单击按钮时调用它。例如buttone\u单击.. 这就是我到目前为止为functionOne public List<string> noDup(List<string>

首先,我的大部分编码经验都是通过python进行的,因此我使用了
global

我有一个列表,我希望能够搜索列表并找到
x
长度的所有单词。我的主要问题是不知道如何从事件侦听器内部调用函数

因此,
myList
是在我的函数
functionOne
中生成的,然后我想在
functionTwo
中使用这个列表,最后我想在单击
按钮时调用它。例如
buttone\u单击..

这就是我到目前为止为
functionOne

public List<string> noDup(List<string> myList)
        {
            var convert = myList.ConvertAll(i => i.ToLower());
            List<string> remove = convertLower.Distinct().ToList();
            return remove;
        }
最后是我的事件侦听器

private void button_Click(object sender, EventArgs e)
        {
            length(remove);
        }
注意:
remove
是我试图处理的列表


但是这个代码没有输出,因为我得到一个错误,说删除在这个上下文中不存在,我不确定我如何使这个列表成为全局的,我们将介绍如何在win forms c应用程序中实现这一点

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        private List<string> _remove;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            length(_remove);
        }

        public List<string> noDup(List<string> myList)
        {
            var convert = myList.ConvertAll(i => i.ToLower());
            _remove = convertLower.Distinct().ToList();

        }

        public List<string> length(List<string> myList)
        {
            int i = int.Parse(lengthSearch.Text);
            List<string> temp = new List<string>();

            foreach (string item in myList)
            {
                if (item.Length == i)
                {
                    temp.Add(item);
                }
            }
            searchResult.Text = string.Join(",", temp);
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Windows.Forms;
命名空间WindowsFormsApp1
{
公共部分类Form1:Form
{
私人名单(删除);;
公共表格1()
{
初始化组件();
}
私有无效按钮1\u单击(对象发送者,事件参数e)
{
长度(_移除);
}
公共列表节点(列表myList)
{
var convert=myList.ConvertAll(i=>i.ToLower());
_remove=convertLower.Distinct().ToList();
}
公共列表长度(列表myList)
{
int i=int.Parse(lengthSearch.Text);
列表温度=新列表();
foreach(myList中的字符串项)
{
如果(item.Length==i)
{
临时添加(项目);
}
}
searchResult.Text=string.Join(“,”,temp);
}
}
}
请注意,由于我刚刚使用了您的代码,所以无法生成此代码。例如,
length
方法应该返回一个
列表
,但它不返回。您可能希望更改传入的列表,然后返回它,或者修改类级变量
\u remove


无论哪种方式,事件处理程序
按钮1\u Click
都需要使用一个类级变量(例如我添加的
\u remove
),或者调用一个将返回列表的方法(如zbignew在另一个类中所示)。

将所有函数放在同一个类中,并在列表实例上使用noDup

    public List<string> noDup(List<string> myList)
        {
            var convert = myList.ConvertAll(i => i.ToLower());
            List<string> remove = convertLower.Distinct().ToList();
            return remove;
        }

    public void length(List<string> myList)
    {
        int i = int.Parse(lengthSearch.Text);
        List<string> temp = new List<string>();

        foreach(string item in myList)
        {
            if(item.Length == i)
            {
                temp.Add(item);
            }
        }
        searchResult.Text = string.Join(",", temp);
    }

        private void button_Click(object sender, EventArgs e)
        {   
            var remove = noDup(yourInputList);
            length(remove);
        }
公共列表节点(列表myList)
{
var convert=myList.ConvertAll(i=>i.ToLower());
List remove=convertLower.Distinct().ToList();
退换货;
}
公共空白长度(列表myList)
{
int i=int.Parse(lengthSearch.Text);
列表温度=新列表();
foreach(myList中的字符串项)
{
如果(item.Length==i)
{
临时添加(项目);
}
}
searchResult.Text=string.Join(“,”,temp);
}
私有无效按钮\u单击(对象发送者,事件参数e)
{   
var remove=noDup(您的输入列表);
长度(删除);
}

创建一个公共方法,该方法返回要使用的列表,然后在需要时调用该方法生成列表。这是语言的基本概念。在尝试用C#编写Python风格的代码之前,先学习一些教程。“学习一些教程”真的吗?这就是你能做的一切吗?如果这是您的输入,我更希望您不注释任何内容;public void functionOne(){myList=new myList(){“a”、“b”、“c”};}好吧,Aphelion认为您需要做一些教程并没有错。如果这个问题有任何迹象表明,你对C#的理解中缺少了两个概念,即了解C#会立即给你这个问题的答案。首先,在C#中没有“全局变量”这样的东西,最接近的方法是在公共类中有一个静态字段。第二,为了使一个对象能够从一个方法之外访问,你需要在该方法之外声明它(例如,作为一个类字段)。而且,这不是我认为的“最佳实践”,但你可能只是想得到一些工作——这很好。当您对它有了感觉后,我绝对建议您查看一些文档(可能在MVP模式model-view-presenter上)。甚至可能还有另一种UI技术—WPF,它有一种不同的、更现代的方法,并且主要使用MVVM模式。
    public List<string> noDup(List<string> myList)
        {
            var convert = myList.ConvertAll(i => i.ToLower());
            List<string> remove = convertLower.Distinct().ToList();
            return remove;
        }

    public void length(List<string> myList)
    {
        int i = int.Parse(lengthSearch.Text);
        List<string> temp = new List<string>();

        foreach(string item in myList)
        {
            if(item.Length == i)
            {
                temp.Add(item);
            }
        }
        searchResult.Text = string.Join(",", temp);
    }

        private void button_Click(object sender, EventArgs e)
        {   
            var remove = noDup(yourInputList);
            length(remove);
        }