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.
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.
– Maniero
I have an object with 20 "equal" fields, in this example would be
Nome1
andNome2
, 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..– LeoHenrique
is that I did fast and this nonsense, I’ll give an improved
– LeoHenrique
I think it’s a little better now
– LeoHenrique
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
@Maniero, I understand he wants to take the values of
Nome1
andNome2
dynamically with afor
, as if in javascript aitem["Nome" + i]
. It became very clear to me after the author’s editions.– Francisco
@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.– Maniero
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''..
– LeoHenrique
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...
– LeoHenrique
Sorry, I misnamed you @Maniero
– LeoHenrique