如何使用索引更新列表集合值? [英] How to update list Collection value with Index?

查看:74
本文介绍了如何使用索引更新列表集合值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我用 Structure 创建一个列表变量来动态添加值。

Hi,
I am creating a list variable with my Structure to add dynamically values.

List<emp> empList = new List<emp>();
 emp em = new emp { 
                ecode=textBox1.Text,
                ename=textBox2.Text
            };
            empList.Add(em);





添加值后,我想更改索引为3的值(或这取决于用户)。

所以我这样做:





After adding the values I want to change the value whose index is 3(or it's depends on user).
so I am trying this:

empList[listBox1.SelectedIndices[0]].ename = textBox3.Text;





但此行给出错误:



but this line gives error:

Cannot modify the return value of 'System.Collections.Generic.List<NewTech.emp>.this[int]' because it is not a variable





我在谷歌上搜索但是找不到合适的结果..



所以我的问题是:

如何用C#中的索引更改List对象值?



感谢先进..



问候

Jayanta。



I am searched on google but cannot find the proper result on this..

So my question is :
how to change the List object value with index in C#?

Thanks in advanced..

Regards
Jayanta.

推荐答案

嗨 - 简单回答:



不要不合适时使用结构 - 使用引用类型 - 所以你的 \\ temp \\ / code>结构应该是一个类,如果y你想改变这样的价值观。

经验法则:永远不要使用结构(或者用相同的结论阅读更长的解释 http://msdn.microsoft.com/en-us/library/ms173109.aspx [ ^ ]



OT:Btw emp 不是一个非常好的类型名称 - 类型名称应该写成大写,没有缩写等。 - 我认为选择通用标准是个好主意,尽管个人名称总比没有好...... http://msdn.microsoft.com/en-us/library/ms229042.aspx [ ^ ]



亲切问候,



Johannes
Hi - Simple answer:

Don't use a struct when not appropiate - use a reference type - so your emp struct should be a class if you want to change the values like this.
Rule of Thumb: Never use structs (or read the longer explaination with the same conclusion http://msdn.microsoft.com/en-us/library/ms173109.aspx[^]

OT: Btw emp is not a very good type name - type names should be written uppercase, no abbreviations etc. - I think it's a good idea to go for "common" standards, though personal ones are better than nothing... http://msdn.microsoft.com/en-us/library/ms229042.aspx[^]

Kind regards,

Johannes


这篇关于如何使用索引更新列表集合值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆