How to simplify code using a loop?

Asked

Viewed 96 times

1

How can I simplify the code below using a loop, I’m in doubt depending on the name of Labels which are sequential, as I put something variable in Fieldname1, NomeCampo2, ..., NomeCampoX.

lblNomeCampo1.Text = dt.Rows[1][0].ToString()+":";
lblCampo1.Text = dt.Rows[1][1].ToString();
lblNomeCampo2.Text = dt.Rows[2][0].ToString() + ":";
lblCampo2.Text = dt.Rows[2][1].ToString();
lblNomeCampo3.Text = dt.Rows[3][0].ToString() + ":";
lblCampo3.Text = dt.Rows[3][1].ToString();
lblNomeCampo4.Text = dt.Rows[4][0].ToString() + ":";
lblCampo4.Text = dt.Rows[4][1].ToString();
lblNomeCampo5.Text = dt.Rows[5][0].ToString() + ":";
lblCampo5.Text = dt.Rows[5][1].ToString();
lblNomeCampo6.Text = dt.Rows[6][0].ToString() + ":";
lblCampo6.Text = dt.Rows[6][1].ToString();
lblNomeCampo7.Text = dt.Rows[7][0].ToString() + ":";
lblCampo7.Text = dt.Rows[7][1].ToString();
lblNomeCampo8.Text = dt.Rows[8][0].ToString() + ":";
lblCampo8.Text = dt.Rows[8][1].ToString();
lblNomeCampo9.Text = dt.Rows[9][0].ToString() + ":";
lblCampo9.Text = dt.Rows[9][1].ToString();
lblNomeCampo10.Text = dt.Rows[10][0].ToString() + ":";
lblCampo10.Text = dt.Rows[10][1].ToString();
  • What language? Explain this code better there.

  • Friend, each language has its particularities. Please mention the language. This is basic in all questions. The more details, the better.

  • Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this, if you haven’t already. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site (when you have enough score).

2 answers

3

Essentially it doesn’t work this way. I’d give with reflection, but I don’t think it pays.

You can restructure the form that handles this by making the fields elements of array also, then everything involving them can be used with a loop. Then you could do so:

lblNomeCampo[i].Text = dt.Rows[i][0].ToString() + ":";
lblCampo[i].Text = dt.Rows[i][1].ToString();

I put in the Github for future reference.

Every time you have several variables with names that have a sequence of numbers as suffix you can replace with a array (or similar structure) in a simple and direct way, that is, just change the fixed number by the index of the array.

Obviously you have to create the array and create the screen objects on top of the elements of array.

Each element of array is a variable like any other, nothing changes in it except the fact that it is accessed by the set of the name plus the index. So treat them this way.

This solution is independent of language, although it would be better to know what it is (from what I understand is C#).

0

It’s not the best way but I could do something like this, I don’t know much about C#, but the way is more or less this:

for (var i=1; i <= 10; i++) {
   Eval("lblNomeCampo"+i+".Text = dt.Rows["+i+"][0].ToString() + \":\";");
   Eval("lblCampo"+i+".Text = dt.Rows["+i+"][1].ToString() + \":\";");
}

I suggest something more elaborate, like:

   foreach (int i in dt.Rows) {
       lblNomeCampo[i].Text = dt.Rows[i][0].ToString() + ":";
       lblCampo[i].Text = dt.Rows[i][1].ToString() + ":";
   }

Leave the fields in the form like this:

name="lblNomeCampo[]"

name="lblCampo[]"
  • 2

    What Eval is this?

  • I’m thinking there must be some way to convert the object using Eval, just like it’s done in javascript.

  • But it doesn’t, at least not in that simple way. And yet, there’s no reason to do it this way when the array works. Even in JS no one should do it.

  • I agree... that’s why I said it’s not the best way.

  • maybe using: myObject.Field = Convert.Changetype(value, fieldType);

  • No. is much more complicated, and goes against the philosophy of language, although it is something bad in any language to use a worse resource when it has a better one that does the same thing.

  • or type this: Type myTypeA = typeof(Myfieldclassa); Fieldinfo myFieldInfo = myTypeA.Getfield("Field");

  • No, it’s no use, it’s not even close. It’s not worth wasting time on something this bad.

  • Well, I published a second option, which I believe is the best... and most elaborate. It goes from the person, what he prefers to use (would not use, in my codes)... as for language, it matters little to me, I’m not the one writing the code. I’m just showing options...

  • You can see that the object itself that he is doing is already within the XGH proposal, to begin with...

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.