开发者

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, i.e. the following code is not valid:

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. I'm pasting the whole code at pastebin, as it's quite long to paste here - custom enumerator attempt. In this code, trying to access the current element by

badClass baddie = ne开发者_高级运维w badClass(ref tmp.Current);

results in an expected error that "A property or indexer may not be passed as an out or ref parameter"

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. 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#. 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. 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. If it does not fit your needs, you have to provide more details.


Why you are using foreach loop. 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. 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜