C#修改结构在List< T> [英] c# modifying structs in a List<T>

查看:112
本文介绍了C#修改结构在List< T>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

短的问题:我如何可以修改的各个项? (以上precisely,存储在列表中的结构成员?)

Short question: How can I modify individual items in a List? (or more precisely, members of a struct stored in a List?)

充分说明:

首先,使用下面的结构定义:

First, the struct definitions used below:

public struct itemInfo
{
		...(Strings, Chars, boring)...
		public String nameStr;
		...(you get the idea, nothing fancy)...
		public String subNum;	//BTW this is the element I'm trying to sort on
}

public struct slotInfo
{
		public Char catID;
		public String sortName;
		public Bitmap mainIcon;
		public IList<itemInfo> subItems;
}

public struct catInfo
{
		public Char catID;
		public String catDesc;
		public IList<slotInfo> items;
		public int numItems;
}

catInfo[] gAllCats = new catInfo[31];

gAllCats填充负载等上下行作为程序运行。

gAllCats is populated on load, and so on down the line as the program runs.

当我想的子项数组中的itemInfo对象进行排序的问题就出现了。
我使用LINQ来做到这一点(因为似乎没有要到非内建类型的列表进行排序任何其他合理方式)。
因此,这里是我有:

The issue arises when I want to sort the itemInfo objects in the subItems array. I'm using LINQ to do this (because there doesn't seem to be any other reasonable way to sort lists of a non-builtin type). So here's what I have:

foreach (slotInfo sInf in gAllCats[c].items)
{
		var sortedSubItems =
				from itemInfo iInf in sInf.subItems
				orderby iInf.subNum ascending
				select iInf;
		IList<itemInfo> sortedSubTemp = new List<itemInfo();
		foreach (itemInfo iInf in sortedSubItems)
		{
				sortedSubTemp.Add(iInf);
		}
		sInf.subItems.Clear();
		sInf.subItems = sortedSubTemp;   // ERROR: see below
}

的错误是,无法修改'SINF的成员,因为它是一个'foreach迭代变量'。

The error is, "Cannot modify members of 'sInf' because it is a 'foreach iteration variable'".

一,这个限制是没有意义的;这不就是在foreach的主要用途构建?

a, this restriction makes no sense; isn't that a primary use of the foreach construct?

B,(也出于赌气)是什么清除()做如果不修改列表? (顺便说一句,名单不会被清除,根据调试器,如果我删除最后一行并运行它。)

b, (also out of spite) what does Clear() do if not modify the list? (BTW, the List does get cleared, according to the debugger, if I remove the last line and run it.)

于是,我就采取不同的方法,看看它的工作使用普通的循环。 (显然,这是因为gAllCats [C] .items实际上是一个IList是唯一允许的。我不认为这将允许您指数常规列表这种方式)

So I tried to take a different approach, and see if it worked using a regular for loop. (Apparently, this is only allowable because gAllCats[c].items is actually an IList; I don't think it will allow you to index a regular List this way.)

for (int s = 0; s < gAllCats[c].items.Count; s++)
{
		var sortedSubItems =
				from itemInfo iInf in gAllCats[c].items[s].subItems
				orderby iInf.subNum ascending
				select iInf;
		IList<itemInfo> sortedSubTemp = new List<itemInfo>();
		foreach (itemInfo iInf in sortedSubItems)
		{
				sortedSubTemp.Add(iInf);
		}
                    //NOTE: the following two lines were incorrect in the original post
		gAllCats[c].items[s].subItems.Clear();
		gAllCats[c].items[s].subItems = sortedSubTemp;   // ERROR: see below

}

这时候,错误的是,不能因为它不是一个变量修改System.Collections.Generic.IList.this [INT]'的返回值。啊!这是什么,如果不是一个变量?并且它什么时候变成了回归价值?

This time, the error is, "Cannot modify the return value of 'System.Collections.Generic.IList.this[int]' because it is not a variable." Ugh! What is it, if not a variable? and when did it become a 'return value'?

我知道必须有一个正确的方式做到这一点;我从C背景来这,我知道我能做到这一点在C(带手动内存管理的好一点虽然)。

I know there has to be a 'correct' way to do this; I'm coming to this from a C background and I know I could do it in C (albeit with a good bit of manual memory management.)

我,四处搜寻,似乎ArrayList的已经过时的支持泛型类型(我使用3.0),并且由于尺寸需要是动态的,我不能使用数组。

I searched around, and it seems that ArrayList has gone out of fashion in favor of generic types (I'm using 3.0) and I can't use an array since the size needs to be dynamic.

谢谢!

推荐答案

纵观for循环的方法,这样做的原因(和解决方案)中的的编译错误文档:

Looking at the for-loop approach, the reason (and solution) for this is given in the documentation for the compilation error:

试图修改值
  类型是作为结果
  中间前pression但并不
  存储在变量。这个错误
  当您尝试直接发生
  修改结构中的通用
  集合。

An attempt was made to modify a value type that is produced as the result of an intermediate expression but is not stored in a variable. This error can occur when you attempt to directly modify a struct in a generic collection.

要修改结构,首先它分配
  到本地变量,修改
  变量,则分配变量
  回集合中的项

To modify the struct, first assign it to a local variable, modify the variable, then assign the variable back to the item in the collection.

所以,在你的for循环,更改以下行:

So, in your for-loop, change the following lines:

catSlots[s].subItems.Clear();
catSlots[s].subItems = sortedSubTemp;   // ERROR: see below

...到:

slotInfo tempSlot = gAllCats[0].items[s];
tempSlot.subItems  = sortedSubTemp;
gAllCats[0].items[s] = tempSlot;

我去掉了调用清除方法,因为我不认为它增加了什么。

I removed the call to the Clear method, since I don't think it adds anything.

这篇关于C#修改结构在List&LT; T&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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