我如何修复 System.IndexOutOfRangeException: 'Index was outside the bounds of the array.' 的撤消和重做?
how can i fix System.IndexOutOfRangeException: 'Index was outside the bounds of the array.' in my undo and redo?
我正在为我的记事本写撤销和重做。这是我的撤销和重做代码,我有一个 System.IndexOutOfRangeException
异常,我该如何解决这个问题?还有其他方法吗?
string[] temp = new string[100];
int index;
int currentpostion;
public Undo()
{
index = 0;
currentpostion = 0;
}
public void Set_Text(string s)
{
temp[index] = s;
currentpostion = index;
++index;
}
public string UndoCons()
{
if (currentpostion > 0)
{
return temp[--currentpostion];
}
return null;
}
public string RedoCosns()
{
if (currentpostion < index)
{
return temp[++currentpostion];
}
return null;
}
}
当数组已满时出现此错误。我能做些什么?谁能改进这个算法?
一个简单的解决方案是使用 List
而不是数组。
List<string> temp = new List<string>();
它需要一些调整,但不会限制您进行 100 次操作。
在任何其他情况下,您应该决定策略。您要删除初始项目吗?然后删除您的前 X 项并用最新的替换它们。
我正在为我的记事本写撤销和重做。这是我的撤销和重做代码,我有一个 System.IndexOutOfRangeException
异常,我该如何解决这个问题?还有其他方法吗?
string[] temp = new string[100];
int index;
int currentpostion;
public Undo()
{
index = 0;
currentpostion = 0;
}
public void Set_Text(string s)
{
temp[index] = s;
currentpostion = index;
++index;
}
public string UndoCons()
{
if (currentpostion > 0)
{
return temp[--currentpostion];
}
return null;
}
public string RedoCosns()
{
if (currentpostion < index)
{
return temp[++currentpostion];
}
return null;
}
}
当数组已满时出现此错误。我能做些什么?谁能改进这个算法?
一个简单的解决方案是使用 List
而不是数组。
List<string> temp = new List<string>();
它需要一些调整,但不会限制您进行 100 次操作。
在任何其他情况下,您应该决定策略。您要删除初始项目吗?然后删除您的前 X 项并用最新的替换它们。