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 = 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”
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:
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.