gpt4 book ai didi

c# - 错误 : "Operation is not valid due to the current state of the object"

转载 作者:行者123 更新时间:2023-11-30 14:51:47 54 4
gpt4 key购买 nike

我的 Unity 项目目前遇到了一个大问题。我试图将一定数量的元素添加到 SortedList,但当我开始游戏时出现此错误:

error: "InvalidOperationException: Operation is not valid due to the current state of the object System.Collections.Generic.SortedList2[TrisCoordinates,System.Boolean].PutImpl
(.TrisCoordinates key, Boolean value, Boolean overwrite)
System.Collections.Generic.SortedList
2[TrisCoordinates,System.Boolean].Add (.TrisCoordinates key, Boolean value)"

代码如下:

void AddCoords() {
coords.Add (new TrisCoordinates (280.62f, 2.217f, 144.84f), false);
coords.Add (new TrisCoordinates (280.65f, 0.9891f, 126.84f), false);
coords.Add (new TrisCoordinates (285.42f, 0.9891f, 129.49f), false);
coords.Add (new TrisCoordinates (281.64f, 2.9645f, 125.05f), false);
coords.Add (new TrisCoordinates (263.00f, 2.2f, 103.27f), false);
coords.Add (new TrisCoordinates (243.29f, 0.9891f, 101.96f), false);
coords.Add (new TrisCoordinates (243.10f, 0.9891f, 99.587f), false);
coords.Add (new TrisCoordinates (233.20f, 1.018f, 93.414f), false);
coords.Add (new TrisCoordinates (249.77f, 0.9891f, 112.95f), false);
coords.Add (new TrisCoordinates (247.87f, 0.9891f, 113.00f), false);
coords.Add (new TrisCoordinates (242.59f, 0.9891f, 112.92f), false);
coords.Add (new TrisCoordinates (240.59f, 0.9891f, 112.99f), false);
coords.Add (new TrisCoordinates (236.85f, 0.9891f, 112.99f), false);
coords.Add (new TrisCoordinates (234.79f, 0.9891f, 112.98f), false);
coords.Add (new TrisCoordinates (248.88f, 0.9891f, 126.71f), false);
coords.Add (new TrisCoordinates (231.15f, 1.0182f, 148.53f), false);
coords.Add (new TrisCoordinates (270.95f, 7.95f, 137.46f), false);
coords.Add (new TrisCoordinates (278.60f, 7.95f, 137.60f), false);
coords.Add (new TrisCoordinates (281.85f, 7.9292f, 137.60f), false);
coords.Add (new TrisCoordinates (270.58f, 7.95f, 135.05f), false);
coords.Add (new TrisCoordinates (264.75f, 9.8372f, 118.75f), false);
coords.Add (new TrisCoordinates (256.27f, 7.93f, 103.37f), false);
coords.Add (new TrisCoordinates (205.63f, 1.037f, 407.70f), false);
coords.Add (new TrisCoordinates (209.02f, 1.037f, 407.70f), false);
coords.Add (new TrisCoordinates (216.61f, 1.037f, 407.70f), false);
coords.Add (new TrisCoordinates (219.56f, 1.037f, 407.70f), false);
coords.Add (new TrisCoordinates (226.32f, 1.037f, 403.22f), false);
coords.Add (new TrisCoordinates (226.03f, 1.037f, 401.04f), false);
coords.Add (new TrisCoordinates (225.98f, 1.037f, 394.01f), false);
coords.Add (new TrisCoordinates (225.97f, 1.037f, 392.41f), false);
coords.Add (new TrisCoordinates (202.00f, 1.037f, 392.56f), false);
coords.Add (new TrisCoordinates (201.03f, 1.037f, 394.67f), false);
}

TrisCoordinates 是一个类:

public class TrisCoordinates {

private float x;
private float y;
private float z;

public TrisCoordinates(float x, float y, float z) {
this.x = x;
this.y = y;
this.z = z;
}

public float GetX() {
return x;
}

public float GetY() {
return y;
}

public float GetZ() {
return z;
}

}

关于它的成因有什么想法吗?

最佳答案

如果您想将该类用作 SortedList 的键,您应该实现 IComparable,因为为了能够排序,排序列表应该能够比较两个 TrisCoordinates 实例。

下面是一个虚拟比较,但它至少应该不会导致您的代码出现异常。

public class TrisCoordinates : IComparable<TrisCoordinates>
{

private float x;
private float y;
private float z;

public TrisCoordinates(float x, float y, float z)
{
this.x = x;
this.y = y;
this.z = z;
}

public float GetX()
{
return x;
}

public float GetY()
{
return y;
}

public float GetZ()
{
return z;
}

public int CompareTo(TrisCoordinates other)
{
return (x + y + z).CompareTo(other.x + other.y + other.z);
}
}

关于c# - 错误 : "Operation is not valid due to the current state of the object",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33495138/

54 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com