0

I'm not sure if this is easy possible in C#. But I would like to get to know how this could be done easily.

public partial class Form1
{
  // I left out the unimportant code for this example
  private myControl cLeft,cTop,cBottom,cRight;
  private List<myControl>mControls;
  public Form1()
  {
  InitializeComponents();
  //this list should contain the fields cLeft,cTop,cBottom,cRight...
  mControls=new List<myControl>(){cLeft,cTop,cBottom,cRight};
  /* now I want that cLeft and so on get assigned...
  of course, this doesn't work because the list refers to the values of
   cLeft ... which are null. So I would need to store a reference to those fields to get this work.*/
  mControls.ForEach(x=>x=new myControl(this));
  }

}

I'm sure it could be done through reflection, but I assume that there should be a way to do this easily in C# or isn't it possible?

leAthlon
  • 734
  • 1
  • 10
  • 22
  • This isn't possible, even with reflection. You'd have to do some trickery with `unsafe` to achieve that. It's also a *really really* bad idea to do this, you should rethink what you're trying to achieve. Variables being changed behind your back through code which appears to have nothing to do with the variables is a recipe for an undebuggable codebase. – Rob Sep 26 '15 at 13:42
  • This looks similar to this question http://stackoverflow.com/questions/7816781/tolist-foreach-in-linq – blogbydev Sep 26 '15 at 13:45
  • @GrantWinney myControl doesn't exist in the designer. This question is not asking about creating controls, it is asking about refering to fields – leAthlon Sep 26 '15 at 13:51
  • @singsuyash not similar. I want to set the variables cLeft and so on through iterating over a list of "references" to them – leAthlon Sep 26 '15 at 13:52
  • 1
    note that i flagged your question as dupe because the other guy is actually trying what you want to achieve. `params with ref`. good idea but it is not possible. – M.kazem Akhgary Sep 26 '15 at 14:18

1 Answers1

0

It's just a simple loop, there is no need to use LINQ. You just need a for loop.

for (int i = 0 ; i < mControls.Count ; i++) {
    mControl[i] = new myControl(this);
}

But, there is no need to write cLeft, cTop etc. You can just refer to them using the indexer: mControls[0], mControl[1] etc.

And remember, the foreach loop or the ForEach extension method doesn't work. This is because you are changing the reference of the variable. That is just another confusing (for beginners) feature of reference types!

Consider this method

public void ChangeReference (string s) {
    s = "Hello";
}

And you call this method:

String s = "xxx";
ChangeReference (s);

Will s be "Hello" after the call? No. In the method, you are changing the location of the string in memory, but the argument is still in the same place!

Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • 1
    I think his main goal is to quickly instantiate the `cLeft`, `cTop`, etc.. variables in a single statement - rather than creating x number of controls. While your solution is most likely the better approach, it's answering a slightly different question – Rob Sep 26 '15 at 13:45
  • @Rob I have never seen someone asks for a single statement to instantiate a lot of objects! And OP is not likely to ask that. Technically, a `foreach` loop is a statement block which is counted as a "statement". – Sweeper Sep 26 '15 at 13:58
  • Just because no one's asked it before, doesn't mean this question isn't asking it :). From the original post: `now I want that cLeft and so on get assigned...` I'm not saying it's a good idea (in fact I left a comment saying the opposite on the question) - but that appears to be what the OP is asking for. – Rob Sep 26 '15 at 14:03
  • @Sweeper , recently I tried asking what rob mentioned. But I had no clue how to describe this properly – leAthlon Sep 26 '15 at 14:03