0

I am creating a kind of interface builder for windows using C# and XAML and was wondering if it was possible to create a variable name out of a string (Label label_string = "Something";). For example, every time a new label is created the variable will be named {label1, label2, label3, ...}. I'm thinking of just creating an array of labels and changing the Name property of each one but I would like to know if my other thought was possible.

4
  • Dynamically complied code might help you ... Commented Jul 17, 2014 at 1:43
  • Just one quick question if I don't choose Dynamically Compiled Code my other array method work? Just before I put the effort into it. And would this method require a complete recompile and link every time a new variable is created? Commented Jul 17, 2014 at 1:46
  • You can somehow simulate that by having a Dictionary<string, YourType> and using it as your "memory". You can make a class with semantics very similar to your initial thought. Commented Jul 17, 2014 at 2:10
  • I used a couple of for loops and a dictionary; works perfectly. Thank you for your suggestion Mephy. Commented Jul 17, 2014 at 4:01

1 Answer 1

1

The short answer is no. If you want to stick with the CLR and strongly typed objects then those objects are defined at compile time and their definition cannot be changed after that.

Now if you want to get more complicated, there are ways to do this using reflection, and emitting new IL/Wrapper classes, and stuff like that. There's also the DLR, where you can use dynamic objects (ala Javascript) in which the definition of the object is defined at run time rather than compile time. But for what you want to do this is probably way more complex and inefficient than just maintaining an array of Label controls where you change the Name property as you described.

UPDATE

It's out of the scope of a SO answer to try and explain how you could actually do this as it's very complicated. But there are several approaches you could research if you are interested.

One of them would be to actually generate c# code and run it through the compiler at run time and then dynamically load that assembly. A quick Bing! search turned up this link that describes doing something like that: http://support.microsoft.com/kb/304655

Another approach would be to use the System.Reflection.Emit namespace to emit CIL instructions in order to build an in memory assembly. Again, a quick Bing! search turned up this link which talks about this kind of thing: http://msdn.microsoft.com/en-us/library/3y322t50(v=vs.110).aspx

Note: the above links aren't exact explanations on how to what you're asking, but they give you an idea about the approach and you will need to do much more research to figure out how to fit it to your specific scenario.

Sign up to request clarification or add additional context in comments.

2 Comments

This is just a quick fun program not a professional one although I know its best to use good practices I would not mind if it was a little inefficient.
Ok ill look at the msdn website on my other computer because it seems that it cannot connect for some reason which is weird because it was working this morning; Thanks for the help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.