Can you assign 4 different values ​​to four labels in one line?

For example, you need to assign values: 24 , 22 , 18 , 8 four labels ( label1 , label2 , label3 , label4 ) in one line?

And another question: How to assign one value to four labels in one line? For example, you need to assign the following labels to four labels: Central Areas .

    3 answers 3

    Why do you need this? The clarity of the code does not improve. But if you really want, then in C # 7+ use

     (label1.Text, label2.Text, label3.Text, label4.Text) = ("24", "22", "18", "8"); 

    Assigning the same value is even easier:

     label1.Text = label2.Text = label3.Text = label4.Text = "Central Areas"; 

    By the way, in the documentation the deconstruction of tuples into already existing variables is mentioned only in passing, and is not shown in any example. Therefore, this opportunity is not so often used.


    Staying within C # 4, you can make such a hack. The assignment label1.Text = "24" is an expression . Expressions can be concatenated to fit on one line, for example, by converting them to the bool type:

     ((label1.Text = "24") == null) & ((label2.Text = "22") == null) & ((label3.Text = "18") == null) & ((label4.Text = "8") == null) 

    So that the compiler does not swear at a separate expression, the result can be assigned to a variable:

     bool dummy = ((label1.Text = "24") == null) & ((label2.Text = "22") == null) & ((label3.Text = "18") == null) & ((label4.Text = "8") == null); 

    (I used & instead of && , because in this case short-circuiting is not used.)

    • Can you elaborate on the first option? What is it? Like this? Where is it from? I seem to have missed something :) - tym32167
    • @ tym32167: Well, this is a tuple deconstruction. A more beautiful example: (a, b) = (b, a); - VladD
    • @ tym32167 is a ValueTuple tuple, and their deconstruction is 4per
    • (label1.Text, label2.Text, label3.Text, label4.Text) = ("24", "22", "18", "8"); Is there something similar for C # 4.0? In more detail, now the code looks like this: labelChance11.Text = "24"; labelChance21.Text = "22"; labelChance31.Text = "18"; labelChance41.Text = "8"; labelChance11.Text = "24"; labelChance21.Text = "22"; labelChance31.Text = "18"; labelChance41.Text = "8"; But you need to write the code in one line - Razi85
    • @ tym32167: So the left side is not a tuple, although it looks the same. - VladD

    Not in one line, of course, but if there are a lot of labels, then you can loop:

     string[] array = new string[4] {"1","2","3","4"}; for(int i = 0; i < 4; i++) this.Controls["label" + (i + 1).ToString()].Text = array[i]; 
    • Then for four labels: labelChance11, labelChance21, labelChance31, labelChance41 how to register a cycle? That's the way to go? string[] array = new string[4] { "24", "22", "18", "8" }; int j = 0; for (int i = 10; i < 42; i+=10, j++) this.groupBox2.Controls["labelChance" + (i + 1)].Text = array[j]; - Razi85
    • one
      @ Razi85, the loop parameters are the same as in my answer, and the body is: this.groupBox2.Controls["labelChance" + (i + 1).ToString() + "1"].Text = array[i]; - Nikolay

    You can still do through the method, although this is not the best option.

     public void SetLabelValue(String s1, String s2, String s3, String s4) { labelChance11.Text = s1; labelChance21.Text = s2; labelChance31.Text = s3; labelChance41.Text = s4; } 

    and then insert in the code where you need to assign labels to the labels:

     SetLabelValue("24", "22", "18", "8");