4

How to concatenate the contents of variable in the name of label and others variables?

//labels: lbl_01_temp, lbl_02_temp, lbl_03_temp
string XX;
double id_01_temp, id_02_temp, id_03_temp;
lbl_XX_temp.Text= "The Device " +XX+ "has" +id_XX_temp+" ℃";  
user1635148
  • 49
  • 1
  • 3
  • 7
  • 1
    Since when does a double have a .Text property? The answers seem to indicate that you're just trying to format the text, but I believe the real question is how to access the correct object? – itsme86 Sep 27 '12 at 00:35
  • 1
    @itsme86 He's referencing a label that he hasn't clearly defined here (lbl_XX_temp). Very similarly named to the doubles declared just above it. – CptSupermrkt Sep 27 '12 at 00:39
  • I'm looking at the answers coming in, and thinking that maybe I'm misunderstanding your question. Are you asking other ways to concatenate (which the answers below sufficiently answer), or are you asking how you can specify a variable name via a string? i.e. if XX is "02", you want lbl_02_temp.Text = "The Device " + 02 + "has " + id_02_temp + " ℃"; ? – CptSupermrkt Sep 27 '12 at 00:43
  • YES, "if XX is "02", you want lbl_02_temp.Text = "The Device " + 02 + "has " + id_02_temp + " ℃";" – user1635148 Sep 27 '12 at 04:20

3 Answers3

7

The clean way to concatenate values is to use String.Format

lbl_XX_temp.Text= String.Format("The Device {0} has {1} ℃", XX, id_XX_temp);

See MSDN Doc: String.Format()

Maybe, I misunderstood the question. I think the OP wants to convert a string into a valid control right?

Web:

string lblSelected = String.Format("lbl_{0}_temp", XX); 
Label lbl = (Label)this.FindControl(lblSelected);
lbl.Text = String.Format("The Device {0} has {1} ℃", XX, id_XX_temp);

WinForms:

string lblSelected = String.Format("lbl_{0}_temp", XX);
Control[] ctrl = this.Controls.Find(lblSelected, true);
Label lbl = ctrl[0] as Label;
lbl.Text = String.Format("The Device {0} has {1} ℃", XX, id_XX_temp);
John Woo
  • 258,903
  • 69
  • 498
  • 492
  • That doesn't address how to access the object to begin with. The object isn't called "lbl_XX_temp". The XX should be replaced with a zero-padded 2-digit number, it seems. – itsme86 Sep 27 '12 at 00:36
  • Yeah that seems to be the case to me as well. In that case OP, how many labels do you have? If this is a localized issue and you only have like five labels, a good ole switch statement for XX would do the quick and simple trick. If you've got like hundreds, you could try putting all the controls into a list and use the index, such that list[Convert.ToInt32(XX)] would give you id_XX_temp. – CptSupermrkt Sep 27 '12 at 00:50
  • Cool. That solution is better than mine. – CptSupermrkt Sep 27 '12 at 00:56
  • @itsme86 yeah you're correct, i totally misunderstood the question. i just updated the answer. – John Woo Sep 27 '12 at 00:57
  • We have already managed to concatenate the name of the labe. string lbl_02=String.Format("lbl_{0}_temp",XX); string lbl_07=String.Format("lbl_{0}_temp",XX); string lbl_10=String.Format("lbl_{0}_temp",XX); Now, I need take what's inside this label. lbl_02_temp="31,6"; XX=02; string lbl_02=String.Format("lbl_{0}_temp",XX); double temp_02=ConvertTo.Double(lbl_02); //I need the value inside the lbl_02_temp or the Double 31.6 – user1635148 Sep 27 '12 at 08:00
  • @John Woo: I think that you addressed only half of the OP's problems, i.e. how to get the right label to modify. But he's got the double vars, too. I'd suggest to put them in a List or Dictionary – Francesco Baruchelli Sep 27 '12 at 09:02
0

String.Format is actually less performing than a straight string concatenation like your question poses. It's probably a matter of ticks and nanoseconds, but there is more overhead involved with String.Format than just saying a + b + c for string concatenation.

Personally, String.Format looks cleaner to me, but it's not faster.

David Hoerster
  • 28,421
  • 8
  • 67
  • 102
0

I would use a pair of arrays or dictionaries (setting them up is left as the proverbial exercise for the reader):

labels[index].Text = "Device " + (index + 1) + " has temperature "
    + temperatures[index].ToString(formatString) + " ℃";

EDIT

From the code in your comment, it seems you want to identify a label by name, and that you will derive the name from an integer variable, like this:

for (int i=1; i<=6; i++)
{
    Label label = GetLabelForIndex(i);
    //do something with the label here
} 

So the question is, how will you get the label for a given index? In other words, what is the implementation of GetLabelForIndex(int)? The answer to that question depends on what kind of Label we're talking about. What library does it come from? Is it WinForms? Silverlight? WPF? If it's WinForms, see Get a Windows Forms control by name in C#. If WPF or Silverlight, see Find WPF control by Name. The accepted answer here also proposes using a dictionary with a string key, which is similar to my suggestion above.

Unless you're doing this thousands of times a second, the performance benefit of using a dictionary is probably insignificant, in which case you should just use Find or FindName directly.

Community
  • 1
  • 1
phoog
  • 42,068
  • 6
  • 79
  • 117
  • Very clever this way to concatenate! The reason of this question is: Get the value that is within the label in which concatenate the name. Please see above. – user1635148 Sep 27 '12 at 08:34
  • @user1635148 please see *what* above? I have a feeling that I haven't quite answered your question, but I don't see why. Perhaps it's because I don't understand where XX comes from. If you could post a complete method, and explain why it doesn't do what you want, your question might be clearer. – phoog Sep 28 '12 at 03:04
  • Forgive my poor explanation. In this app I need to get the label.Text within loops. for(int i=1;i<=6;i++) { // I need the L_HM_0i_02.Text, // L_HM_0i_07.Text and L_HM_0i_10.Text mx_02=String.Format("L_HM_0{0}_02",i);//("L_HM_0{0}_02",i).Text mx_07=String.Format("L_HM_0{0}_07",i); mx_10=String.Format("L_HM_0{0}_10",i); } – user1635148 Sep 28 '12 at 11:07
  • @user1635148 I have edited the answer in response to your comment. – phoog Sep 28 '12 at 14:27
  • Thank you phoog, I upload sample code to this link: http://www.sendspace.com/file/n3gkyd – user1635148 Oct 01 '12 at 09:09