• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

C# string into object like conversion?

Status
Not open for further replies.
Is it possible in C# to use textmacros like in vjass code?

For example, I want to send an integer into function.
And then that function use this argument in object names within it.
Code:
some_function(string text)
{
     $text$.Text = "something like this";
     variable_$text$ = 1; // + this
     variable[$text$] = whatever; // and/or even something like this...
}

EDIT:

I need to solve next problem:
Code:
func(Button b1, Button b2, Label l1, Label l2)
{
     b1.Text = "some text 1";
     b1.Text = "some text 1";
     l1.Text = "some text 2";
     l2.Text = "some text 2";
     //...
}

//some code
//...
func(button_x_1, button_y_1, label_x_1, label_y_1);
func(button_x_2, button_y_2, label_x_2, label_y_2);
func(button_x_3, button_y_3, label_x_3, label_y_3);
//...


//==================================================
func( ID )
{
     button_x_$ID$.Text = "some text 1";
     button_y_$ID$.Text = "some text 1";
     label_x_$ID$.Text = "some text 2";
     label_y_$ID$.Text = "some text 2";
     //...
}

func(1);
func(2);
func(3);
Original code is like 20 times larger that this here.
Everything is working fine, I just need to know is they any way around.

Because all I need is to send this simple integer (call it ID) that will then edit all those objects:
button_x_$ID$
button_y_$ID$
label_x_$ID$
label_y_$ID$

Only way I see here, as some sort of solution, is to make array:
B_X[1] = button_x_1;
B_X[2] = button_x_2;
B_X[3] = button_x_3;
B_Y[1] = button_y_1;
B_Y[2] = button_y_2;
B_Y[3] = button_y_3;
etc

But that create other problems like static and non static class members, I need to create huge lists of those array initialization and so on.
 
Last edited:
Level 15
Joined
Oct 18, 2008
Messages
1,588
How about something like:
C#:
this.Controls.Find("button_x_"+ID, true);

Should work on WinForms, and when it comes to Silverlight and Windows Phone, you can just use MVVM instead by binding the text to an element. Or use VisualTreeHelper/custom recursive function to get the needed control.
 
Last edited:
Level 14
Joined
Jan 5, 2009
Messages
1,127
@Crazed_seal2: better use this.Controls.Find("button_x_"+ID, true); - it's an inbuilt function, which most likely means the CL code is more optimized.

Also it won't work with Windows Phone or Silverlight, those need alternative solutions.

That find function returns an array. You're going to have to loop through it, or check if the array has more than one item.
 
That find function returns an array. You're going to have to loop through it, or check if the array has more than one item.
Exactly, it's much easier to do it the way @Crazed_seal2 shown. Dunno how I didn't think of that earlier...
Code:
public Control GetButton(string prefix ,int i, string sufix)
{
     foreach (Control c in this.Controls)
     {
          if (c.Name == prefix + Convert.ToString(i) + sufix)
          {
               return c;
          }
     }
     return null;
}

//...

    //... so something like...
    Button b = GetButton("whatever", 1, "") as Button;
    //...
 
Level 29
Joined
Jul 29, 2007
Messages
5,174
Like I said in the chat, use a map. Maps were meant for exactly this usage.

C#:
Dictionary<string, Control> controlDict = new Dictionary<string, Control>();

foreach (Control c in this.Controls)
{
    controlDict.Add(c.Name, c);
}

...

Control something = controlDict["something"];
 
Status
Not open for further replies.
Top