简体   繁体   中英

c# how do i make my list 1 based rather than 0 based

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

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.

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.

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.

您可以定义自己的列表类,以扩展内置类并将第一个元素插入构造函数中。

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);

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";   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM