List item for loop variable C#

Asked

Viewed 71 times

1

Is there any way I can make one for or foreach in a list and within this I create variables for my obj items

EXAMPLE:

public class Pessoa
{
   public int ID {get;set;}
   public string Nome1 {get;set;}
   public string Nome2 {get;set;}

   public List<Pessoa> lstPessoa{get;set;}
}

foreach (var item in lstPessoa)
{   
    for(int i = 1; i < 3; i++)
    {
        // GOSTARIA DE SABER SE É POSSIVEL, DE ALGUMA MANEIRA, FAZER ISSO
        var exemplo = "item.Nome"+i; 

        mtdSalvarBanco(exemplo);
        cont++;
    }
}

Nome1 = Joao

Nome2 = Maria

var exemplo = "item.Nome"+i;

i = 1 > exemplo = Joao

i = 2 > exemplo = Maria

other than

var exemplo1 = item.Nome1;

var exemplo2 = item.Nome2;

exemplo1 = Joao

exemplo2 = Maria

There’s nothing more I can say, it’s just this doubt of mine... I don’t know how else to write

  • The hard part is we understand what we want to do, and if you’re having a hard time with what you’re doing, or if what you did doesn’t answer. Explain it better so we can help. Maybe you don’t even need it and you still don’t know.

  • I have an object with 20 "equal" fields, in this example would be Nome1 and Nome2, and I need to save every single one of them in the bank.. My question is whether there is any way, with a counter for example, I pass the value of each item of this my object to a variable, so that I can use it later..

  • is that I did fast and this nonsense, I’ll give an improved

  • I think it’s a little better now

  • You still don’t know what you want or what problem you’re having. You have to say this. It’s actually gotten worse. The code is all dropped, meaningless and unexplained.

  • @Maniero, I understand he wants to take the values of Nome1 and Nome2 dynamically with a for, as if in javascript a item["Nome" + i]. It became very clear to me after the author’s editions.

  • @Francis I’m not sure, but if this is so it needs to be put in the question clearly, because otherwise each one interprets as he wants and each one has a solution, as I ended up doing and erred. What I understand is that even he doesn’t know what he wants. If so, the solution is simple, don’t use the for, It takes more work and it costs more to do so than to catch each one of them. If his problem is another, q is highly unlikely (because it would even be a conceptual error) then it would be the case to make an aggregation.

  • Forgive me for not being able to express myself, but that’s exactly what @Francisco said above.. I think few people have the knowledge that you have 'Miner', I believe I do not reach 10% of what you know, so I would like a little more respect about the way you have referred to me.. I can have a lot of difficulty expressing myself and that’s something I’ve been dealing with for years, if you can’t understand, even though I sometimes say that it’s hard for me to exemplify otherwise, so at least have some respect in your speech before me.. Your talk offends, I felt 'humiliated''..

  • In the same way that you say that people interpret it as they want, I interpreted your speech as disrespect, so before you ask me to improve my code, improve the way you talk to me...

  • Sorry, I misnamed you @Maniero

Show 5 more comments

1 answer

1

I think that’s kind of what you want:

using static System.Console;
using System.Collections.Generic;

public class Program {
    public static void Main() {
        Pessoa.Lista();
    }
}

public class Pessoa {
    public int ID { get; set; }
    public string Nome1 { get; set; }
    public string Nome2 { get; set; }
    public static List<Pessoa> Pessoas { get; set; } = new List<Pessoa>();
    public static void Lista() {
        for (var i = 0; i < Pessoas.Count; i++) WriteLine(Pessoas[i].Nome1 + (i + 1).ToString());
    }
}

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

There are several errors in this code.

If you count the items the best way is to use one for crude, but not the foreach, it makes no sense to use both. So it goes from index 0 to the list element counter which is the property Count.

Whether to access each item and create one string, do not put inside the quotes because then it becomes text and you want it to be code. You have to take the number of the counter and turn it into string, but as the list starts from 0, you must add 1, not forgetting to be in parentheses so that the ToString() apply to the sum and not to the 1 only.

There is still a conceptual error. Why does a person have people inside him? It doesn’t make sense, it should be somewhere else. But not to change too solved at least the most serious problems. I turned this into a static member, so it becomes part of the class and not the instance, so at least now you have the list once in the entire application and not a list for every person you create.

You also need to initialize the list, the way it was going to error in the first access to it. I also changed the name to meet the nomenclature normally adopted in C#.

I took advantage and modernized and formatted the code better.

The impression is that you are having very basic difficulty, so my suggestion is to focus on more basic aspects of the language before venturing into more complex things. Build your knowledge one step at a time without skipping anything, without leaving anything behind, then you will enjoy much more and understand what you are doing.

  • That’s not what I want, it’s complicated I can’t explain otherwise, I’ll try, but by adopting that I’m layman, try to understand in a layman’s way.. I want to loop an item of my object dynamically, as I am currently making a block repeat 20 times understand: var example = item.Name1; var example = item.Name2; var example = item.Name3; var example = item.Name4; var example = item.Name5; however I would like to make a single block and pass all these items (which are in a list)

  • @Leooso create another list like this: public List<string> Nomes = new List<string>();. And make a foreach within this list, whenever there is a name, you will iterate over it. And just use Nomes.Add("NOVO NOME"); to add a new

  • Wow, it’s true... I have this problem, I make things simple into complicated, being that if I started thinking for the simple I would solve.... I’ll try here and give you feedback of the result, thank you

Browser other questions tagged

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