How to set the Index of a List to start at 1 instead of 0 when populated

Asked

Viewed 142 times

0

Whenever I populate a list in C#, as in the example below, as I add items, they are arranged starting at position 0 in index. If I add 5 items to my list, I will have the positions 0, 1, 2, 3 and 4. I need to get the index start with 1 and be in order 1, 2, 3, 4 and 5.

That’s possible?

List<PessoaContatoViewModel> lista = new List<PessoaContatoViewModel>();
lista.Add(pc);
  • Why do you need it? I see no reason for it.

  • I do, because I’m creating some fields dynamically and I’m testing a way to pass a List with the positions ready @Maniero.

  • Explain better, I haven’t seen the reasons yet, it doesn’t make a difference starting at 0 or 1, you can do everything you need. Give a real example.

  • @Masterjr Don’t just control the real index separately? If the indexes start at 0 and you need to control how 1 only increment by 1 each index...

  • Think of a screen that you need to insert phone fields dynamically without using JS. Imagine that you already have two fields created... then, the index is in 1, because the list goes from 0 to 1, so logically, you have two items.

  • ...If I add a third field the index will be in 2 and if I pass a list with a headset model the position of it will be in 0 and I will need to pass with index 2. So I need to do this in order to test if it works... I know you could say: Oh, but you could simplify this using JAVA Script..., but I don’t know much and I saw problems when creating fields using JS in the validations part, because they don’t work and have to make a lot of modifications that in my case would take time...

  • I don’t understand anything at all. Anyway, I can’t imagine any reason to want to do this.

  • kkkkkkkkkkkkkkk If I could set this, it would be enough and solve my problem.

  • @Masterjr But can not, there is no such option. I ask what the goal because there is no reason to do this... Understanding the initial reason I could tell you a solution.

  • Got it... From all of you, thank you @Maniero :)

  • Have you thought about adding an empty item to your list before filling it, where it will take the 0 position. And when you start the loop for, start the index count by counting 1?

Show 6 more comments

1 answer

4

It’s not possible, and it’s not because it doesn’t make sense, it’s not necessary. Using your example, let’s assume you have an i that starts at 1, just access by subtracting 1 and it’s solved, you’ll access the first element as you want using a variable that starts at 1:

var i = 1;
var lista = new List<PessoaContatoViewModel>();
lista.Add(pc);
Write(lista[i - 1]);

I put in the Github for future reference.

If you can’t figure that out or solve the issue, or explain if that’s not what you need, then the problem is that you’re trying to do what you’re doing right now, beyond what you’re capable of doing and it’s not an answer here that will solve the problem. Then the suggestion is to start doing simpler things, build knowledge step by step, to get to the point you need, skip steps does not help.

  • Thank you @Maniero! Unfortunately, since there is no change, I will look for another solution. Hug!

Browser other questions tagged

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