简体   繁体   English

C#-如何创建可更新的枚举器?

[英]C# - How to create updatable enumerator?

I'm a bit new to C# (coming from PHP) and I was a bit shocked that, by looping through a list I can't pass a reference to that varialbe, ie the following code is not valid: 我是C#的新手(来自PHP),但令我感到震惊的是,通过遍历列表,我无法传递对该变量的引用,即以下代码无效:

foreach (ref string var in arr) {
    var = "new value";
}

I researched a bit and I found a suggestion to create a "updatable enumerator" but I can't figure out how exactly I should do that. 我研究了一下, 发现了创建“可更新的枚举数”的建议,但我不知道该怎么做。 I followed an example and tried to add a setter for the Current for both the IEnumerator.Current method and my custom enumerator (PeopleEnum.Current), but, to be honest, that was blind guessing and didn't work. 我遵循了一个示例,尝试为IEnumerator.Current方法和我的自定义枚举器(PeopleEnum.Current)都为Current添加一个setter,但是,老实说,这是盲目的猜测,没有用。 I'm pasting the whole code at pastebin, as it's quite long to paste here - custom enumerator attempt . 我将整个代码粘贴在pastebin上,因为粘贴到这里很长- 自定义枚举器try In this code, trying to access the current element by 在这段代码中,尝试通过以下方式访问当前元素

badClass baddie = new badClass(ref tmp.Current);

results in an expected error that "A property or indexer may not be passed as an out or ref parameter" 导致出现预期的错误,即“属性或索引器可能无法作为out或ref参数传递”

What I'm aiming to do in the end is something like this - iterate through a list of objects, generate a button for each of them and add an onclick event for that button which will open a new form, passing the reference for that object, so that its contents can be edited in that new form. 最后,我打算做的是-遍历对象列表,为每个对象生成一个按钮,并为该按钮添加onclick事件,这将打开一个新表单,并传递该对象的引用,以便其内容可以以该新形式进行编辑。 I did all this, but passing the object as a reference, instead of read-only data, is killing me. 我做了所有这些工作,但是将对象作为参考而不是只读数据传递给了我。 I would appreciate any answers, links where I can read about updatable enumerators or ideas. 我希望您能从中获得有关可更新的枚举器或想法的任何答案,链接。

First of all - without wanting to blame you - I would say: If you learn a new language, learn the new language! 首先-不想怪您-我会说:如果您学习一种新语言,请学习这种新语言! And don't try to develop PHP using C#. 并且不要尝试使用C#开发PHP。 If computer languages would all be the same, we would not have so much of them. 如果所有计算机语言都相同,我们将没有太多。 ;-) ;-)

I don't see exactly how your example is related to the actual job you want to do, but you shoudl probably learn about events, delegates and LINQ first. 我看不出您的示例与您要完成的实际工作有何关系,但是您应该首先了解事件,委托和LINQ。 Might something like this help: 可能会得到以下帮助:

foreach (Obj obj in yourBaseObjects) {
    Obj localObj = obj; // See Dans comment!!!
    Button button = new Button(); // however you create your buttons
    button.Click += {
         // do something with obj
         Console.WriteLine(localObj);
    }
}

Yes, that works in C# and each event handler will be using the correct object. 是的,这在C#中有效,并且每个事件处理程序都将使用正确的对象。 If it does not fit your needs, you have to provide more details. 如果它不符合您的需求,则必须提供更多详细信息。

Why you are using foreach loop. 为什么要使用foreach循环。 Use for loop. 用于循环。 I dont know the exact code/syntax but something like that: 我不知道确切的代码/语法,但类似的东西:

int sizeOfArray=objectArray.size();
for(int i=0;i<sizeOfArray;i++)
{
    obj=objectArray[i];
    // use obj whatever you wany
}

It sounds like you're not trying to pass a reference to the Object (the Object is already a reference type), but rather a reference to the Object's location in the array, correct? 听起来您不是要传递对对象的引用(对象已经是引用类型),而是对对象在数组中位置的引用,对吗? This latter is not directly possible in .NET, due to the way it manages memory and references. 由于后者管理内存和引用的方式,后者在.NET中不可能直接实现。 You can accomplish something like it using a wrapper class (no error handling, but this is the basic idea): 您可以使用包装器类完成类似的操作(没有错误处理,但这是基本思想):

public sealed class ListReference<T>
{
    private readonly IList<T> _list;
    private readonly int _index;

    public ListReference(IList<T> list, int index)
    {
        _list = list;
        _index = index;
    }

    public T Value
    {
        get { return _list[_index]; }
        set { _list[_index] = value; }
    }
}

You can now construct this and pass it along, with all the associated complexity risks that come with passing around multiple references to an array. 现在,您可以构造它并传递它,以及将多个引用传递给数组带来的所有相关的复杂性风险。 It would be better to change the design to avoid this, if possible, but it is possible to accomplish what you're after. 如果可能的话,最好避免更改设计,但是可以实现您所追求的目标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM