Pass values to 99 fields in a loop

Asked

Viewed 147 times

-1

I would like to pass values to 99 fields in one loop;

For example:

for(int i=1; i<= 99 ; i++)
{
    nomedocampo + "i" = "valorqualquer";
}
  • Which fields, question missing context?

  • are Labels with the same name but with the number from 1 to 99 concatenated

  • Is it a WPF? Mvc? Winforms? Depending on the environment, you can use "find control" methods that would make your life easier. Or depending on the use Reflection

  • Web application

2 answers

6


The problem presented should not be solved like this, there is a simple and basic mechanism called array. With it you do what you want in a simple and correct way, with good performance and no chance of mistakes. This can be used in any scenario, including those cited in comments on this page.

No matter what I wanted to do, this is the right solution. It is not that it is a better alternative, any other way very different from this is a complete nonsense. Using reflection for something so basic is probably the worst mistake I’ve ever seen using reflection because it has something better in every way possible.

The solution with dictionary is not an absurdity for some cases (although the code could be better), but is not good for this, the array simple solves better. What can be used in place of the array is a list, it depends on whether you can have a varied number of fields or not.

Look how simple:

using static System.Console;

public class Program {
    public static void Main() {
        var objeto = new Classe();
        for (int i = 0; i < 99 ; i++) objeto.campos[i] = "valorqualquer" + i;
        foreach (var item in objeto.campos) WriteLine(item);
    }
    public class Classe {   
        public string[] campos = new string[99];
    }
}

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

Obviously each individual variable of the array (its elements) can be accessed directly with her number and does not need to use a variable, so instead of having campo1 will have campo[1], only this.

2

In C# you cannot create variables with dynamic names because the language is strongly typed. What you can do is create an array with these fields, the way recommended by MICROSOFT is that you use a dictionary, as link: MICROSFOT.

Below is an example of creating a dictionary:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QuickTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Dictionary<string, int> names = new Dictionary<string,int>();


            for (int i = 0; i < 10; i++)
            {
                names.Add(String.Format("name{0}", i.ToString()), i);
            }

            var xx1 = names["name1"];
            var xx2 = names["name2"];
            var xx3 = names["name3"];
        }
    }
}
  • thanks for the answer! So, in fact these 99 fields already exist, they have the same name but with the number from 1 to 99 concatenated. What I need to do is pass values using a loop without having to do this 99x procedure.

  • In this case, you will not be able to assign in a variable, C# does not allow this type of assignment. You would have to create the array or dictionary. With the array you can assign the values as follows: Names["name" + i] = 'potato';

  • 2

    He doesn’t need create variables, just change the value of the ones that are created. This is possible using Reflection

Browser other questions tagged

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