Reputation: 2028
I have a list and i have added my winform textboxes to it as shown below, but the list is 0 based, as the count starts from 0, how do i make it 1 based?
List<TextBox> textBoxList = new List<TextBox>();
textBoxList.Add(textBox1);
textBoxList.Add(textBox2);
textBoxList.Add(textBox3);
textBoxList.Add(textBox4);
textBoxList.Add(textBox5);
textBoxList.Add(textBox6);
textBoxList.Add(textBox7);
textBoxList.Add(textBox8);
textBoxList.Add(textBox9);
textBoxList.Add(textBox10);
textBoxList.Add(textBox11);
textBoxList.Add(textBox12);
I have an index which can be any number from 1 to 12, illd like to use this index to find the right textbox so index 6 will be textbox 6 .. rather than textbox 5
Upvotes: 1
Views: 8308
Reputation: 15981
One rather awkward way to solve your problem of making the collection index 1-based is to use the Array class. You can then create an array instance with arbitrary lower bound. In your case it would look like this:
var textBoxList = Array.CreateInstance(typeof(TextBox), new[] {12}, new[] {1});
Defining the elements in this array is somewhat more cumbersome:
...
textBoxList.SetValue(textBox5, 5);
...
And similarly, accessing the elements is also rather explicit:
var tb9ref = (TextBox)textBoxList.GetValue(9);
Upvotes: 0
Reputation: 2028
heres what i did and it seems to work :D, simple enough i guess.. still learning ..
var tb = textBoxList;
int newDef = def - 1;
tb[newDef].Text = "occupied";
Upvotes: -1
Reputation: 581
Maybe in your case it will be better to use Dictionary? So, your code will transform to:
Dictionary<int, TextBox> textBoxes =
new Dictionary<int, TextBox>();
textBoxes.Add(1, textBox1);
But anyway it's a strange requirement.
Upvotes: 1
Reputation: 45058
Things don't do this way - list/array access is inherently zero-index based, and a custom implementation to attempt to defy this would be confusing (I personally think naming a class to be descriptive of its nature, if this were its nature, would be harder than doing the logic for a specific case elsewhere).
What's the use case? Usually you can get by with incrementing an index value to represent a 'current', or 'count' value that would make sense to users, or maybe adjusting input from a user perspective to what is needed, but what is your specific case?
If what you want to do, based on your update, is get a control by index you can either start your index from 0 or adjust the input as mentioned (by decreasing it according to the offset you need in the array) - this is just as easy, if not easier, and straightforward enough for anyone to understand, as any other method.
Upvotes: 2
Reputation: 21
You can define your own list class which extends the built in class and insert the first element in the construct function.
Upvotes: 0
Reputation: 726809
You cannot make .NET's list one-based, but you can either (1) adjust your index down by one every time you read, or (2) insert a useless entry at position 0
and ignore it after that (not recommended).
You can inherit from list, and build your own data structure that adjusts indexes on the way in and out, but that would require a lot more effort, not to mention the amount of confusion among the readers, so I would strongly caution against doing that.
Upvotes: 3