在通用用户定义列表中覆盖现有项目。

编程入门 行业动态 更新时间:2024-10-24 17:25:35
本文介绍了在通用用户定义列表中覆盖现有项目。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有用户定义的类包含三个公共字符串类型变量。我正在创建它的列表,以便添加该类型的任意数量的项目。但是在添加list.Add(item)列表时,将最后一项写入其中。以下代码 公共类QuizSearchListVariable { public string quiz_type_id; 公共字符串quiz_type_title; 公共字符串quiz_type_description; public void清除() { quiz_type_id = string.Empty; quiz_type_title = string.Empty; quiz_type_description = string.Empty; } 公共覆盖字符串ToString() { 返回string.Format([QuizSearchListVariable] \ n Quiz_Type_ID = {0} \ n Quiz_Type_Title = {1} \ n Quiz_Type_Description = {2},quiz_type_id,quiz_type_title,quiz_type_description); } 我尝试过: /从这里添加物品 for(int i = 0; i< data [quiz_list]。Count; i ++){ Obj1.Clear(); Obj1.quiz_type_id = (字符串)data [quiz_list] [i] [id ]; Obj1.quiz_type_title =(字符串)data [quiz_list] [i] [title]; Obj1.quiz_type_description =(字符串)数据[ quiz_list] [i] [description]; MyQuizList.Add(Obj1); QuizTitleList.Add(data [quiz_list] [i] [ title]。ToString()。ToLower());

解决方案

您的代码重复使用类的一个实例'Obj1,并添加相同的实例到列表。是的,你清除那个'Obj1实例的字段,然后写新值,但是,引用到'Obj1保持不变;最后,你有多个引用'列表中的Obj1'的MyQuizList,并且它们都包含你写的最后一个值。 所以,你需要创建循环的每次迭代都是类的新实例: Obj1 = new QuizSearchListVariable();

继续将相同的引用对象传递给列表。需要每次都创建一个新对象并用新字段填充它,否则之前的对象将具有新值,因为它只是一个引用。

你需要了解引用变量的工作原理

for ( int i = 0 ; i < data [ quiz_list]。计数; i ++){ Obj1.Clear(); // ... MyQuizList.Add(Obj1);

只有一个Obj1副本,你正在清理它的数据,设置它的数据并将它添加到列表中,但是你只是多次添加同一个对象,你没有该物品的不同副本。 想象一下,你有一个袋子,在你的袋子内放置方形,你会让某人抓住它。然后你清理袋子,这样你就移开了方形,然后在里面加上圆圈并告诉别人拿着袋子。所以你有一个袋子,两个人都拿着,那个袋子拿着一个圆圈。 你真正想做的是创造一个新的包而不是清除现有的包,所以你有一个带方形的袋子和一个带圆圈的袋子。所以用创建一个新实例替换Clear。

for ( int i = 0 ; i < 数据[ quiz_list]。计数; i ++){ Obj1 = new QuizSearchListVariable(); // ... MyQuizList.Add(Obj1);

I have user-defined class contains three public string type variables. I am creating its list so as to add as many items of that type. but on adding list.Add(item) list is over writing last item to each of them. following code public class QuizSearchListVariable{ public string quiz_type_id; public string quiz_type_title; public string quiz_type_description; public void Clear() { quiz_type_id = string.Empty; quiz_type_title = string.Empty; quiz_type_description = string.Empty; } public override string ToString () { return string.Format ("[QuizSearchListVariable] \n Quiz_Type_ID={0} \n Quiz_Type_Title={1} \n Quiz_Type_Description={2}", quiz_type_id, quiz_type_title, quiz_type_description); } What I have tried: / Adding items from here for (int i = 0; i < data ["quiz_list"].Count; i++) { Obj1.Clear (); Obj1.quiz_type_id = (string)data ["quiz_list"] [i] ["id"]; Obj1.quiz_type_title = (string)data ["quiz_list"] [i] ["title"]; Obj1.quiz_type_description = (string)data ["quiz_list"] [i] ["description"]; MyQuizList.Add (Obj1); QuizTitleList.Add (data ["quiz_list"] [i] ["title"].ToString ().ToLower ());

解决方案

Your code is using one instance of the Class, 'Obj1, repeatedly, and adding that same instance to the List. Yes, you clear the fields of that instance of 'Obj1, and write new values, but, the reference to 'Obj1 stays the same; at the end, you have multiple references to 'Obj1 in your List 'MyQuizList, and all of them contain the last values you wrote. So, you need to create a new instance of your Class with each iteration of the loop: Obj1 = new QuizSearchListVariable();

you keep passing the same referenced object to the list. need to create a new object every time and populate it with the new fields, otherwise the previous objects will have the new values since it's only a reference.

You need to understand how reference variables work

for (int i = 0; i < data ["quiz_list"].Count; i++) { Obj1.Clear (); // ... MyQuizList.Add (Obj1);

There is only one copy of Obj1, and you are clearing its data, setting its data and adding it to the list, however you are only adding the same object multiple times, you don't have different copies of the item. Imagine you have a bag and inside that bag you put "square", and you get someone to hold that bad. You then clear the bag so you remove square and you put "circle" in it and tell someone else to hold the bag. So you have one bag that both people are holding and that bag holds a circle. What you really want to do is create a new bag rather than clear your existing one, so you have one bag with a square and one with a circle. So replace Clear with creating a new instance.

for (int i = 0; i < data ["quiz_list"].Count; i++) { Obj1 = new QuizSearchListVariable(); // ... MyQuizList.Add (Obj1);

更多推荐

在通用用户定义列表中覆盖现有项目。

本文发布于:2023-11-27 16:02:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1638669.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:定义   项目   用户   列表中

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!