超载索引器不工作
我有一个自定义集合,我想超载索引器。我的问题是,即使签名不同,我也会收到编译时错误。
public HeaderLine this[LineNumber lineNum]
{
get
{
return (HeaderLine)InnerList[(int)lineNum - 1];
}
set
{
InnerList[(int)lineNum] = (HeaderLine)value;
}
}
public HeaderLine this[int lineNum]
{
get
{
return (HeaderLine)InnerList[lineNum - 1];
}
set
{
InnerList[lineNum - 1] = (HeaderLine)value;
}
}
}
当我只是尝试实现这个时:
MessageBox.Show(myDatafile.Header[3].Description);
这些是错误:
错误“XXX.Header.this[XXX.LineNumber]”的最佳重载方法匹配有一些无效参数
错误参数“1”:无法从“int”转换为“XXX.LineNumber”
我开始认为您不能多次超载索引器?或者我做错了什么而这不起作用。 我知道解决方法,例如 XXX.Header[(LineNumber)myIntegerValue],但我真的希望它对新手非常友好。谢谢。
编辑 LineNumber
是一个枚举
长编辑 这是我的自定义集合类(抱歉,如果太长我没有知道什么是必要的)
namespace XXX
{
public class HeaderLines : CollectionBase
{
public void Add(HeaderLine newHeaderLine)
{
List.Add((HeaderLine)newHeaderLine);
}
public void Remove(HeaderLine newHeaderLine)
{
List.Remove((HeaderLine)newHeaderLine);
}
public HeaderLine this[LineNumber lineNum]
{
get
{
return (HeaderLine)InnerList[(int)lineNum - 1];
}
set
{
InnerList[(int)lineNum] = (HeaderLine)value;
}
}
public HeaderLine this[int lineNum]
{
get
{
return (HeaderLine)InnerList[lineNum - 1];
}
set
{
InnerList[lineNum - 1] = (HeaderLine)value;
}
}
}
}
namespace XXX
{
public class HeaderLine
{
public string Description { get; set; }
public override string ToString()
{
return Description;
}
}
}
编辑:我很抱歉浪费了大家的时间。我发现了我的问题,这是一个愚蠢的错误。我刚刚开始通过阅读自学。发生的情况是保存集合的类没有正确实现索引器。对不起。
I have a custom collection and I want to overload the indexer. My problem is that I get a compile time error even though the signatures are different.
public HeaderLine this[LineNumber lineNum]
{
get
{
return (HeaderLine)InnerList[(int)lineNum - 1];
}
set
{
InnerList[(int)lineNum] = (HeaderLine)value;
}
}
public HeaderLine this[int lineNum]
{
get
{
return (HeaderLine)InnerList[lineNum - 1];
}
set
{
InnerList[lineNum - 1] = (HeaderLine)value;
}
}
}
And when I simply try to implement this:
MessageBox.Show(myDatafile.Header[3].Description);
These are the errors:
ERROR The best overloaded method match for 'XXX.Header.this[XXX.LineNumber]' has some invalid arguments
ERROR Argument '1': cannot convert from 'int' to 'XXX.LineNumber'
I'm starting to think that you can't overload an indexer multiple times? Or what am I doing wrong where this doesn't work. I KNOW WORKAROUNDS such as XXX.Header[(LineNumber)myIntegerValue], but I'd really like it to be VERY novice friendly. Thank you.
EDIT LineNumber
is an enum
LONG EDIT Here is my custom collection class (sorry if too long I didn't know what would be necessary)
namespace XXX
{
public class HeaderLines : CollectionBase
{
public void Add(HeaderLine newHeaderLine)
{
List.Add((HeaderLine)newHeaderLine);
}
public void Remove(HeaderLine newHeaderLine)
{
List.Remove((HeaderLine)newHeaderLine);
}
public HeaderLine this[LineNumber lineNum]
{
get
{
return (HeaderLine)InnerList[(int)lineNum - 1];
}
set
{
InnerList[(int)lineNum] = (HeaderLine)value;
}
}
public HeaderLine this[int lineNum]
{
get
{
return (HeaderLine)InnerList[lineNum - 1];
}
set
{
InnerList[lineNum - 1] = (HeaderLine)value;
}
}
}
}
namespace XXX
{
public class HeaderLine
{
public string Description { get; set; }
public override string ToString()
{
return Description;
}
}
}
EDIT: I feel so sorry to waste everyones time. I found my problem and it was a stupid mistake. I'm just beginning to teach myself through reading. What happened was the class that held the collection didn't correctly implement the indexers. Sorry.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您绝对可以超载索引器(许多系统类型都会这样做)。这是一个编译没有问题的例子:
看看你是否能想出一个类似的简短但完整的程序,但它失败了,我们可以找出原因。目前存在太多未知因素,无法轻松诊断问题。
You most definitely can overload indexers (and plenty of system types do so). Here's an example which compiles without problem:
See whether you can come up with a similar short but complete program which fails, and we can work out why. At the moment there are too many unknowns to easily diagnose the problem.
这是一个示例,再次显示多个索引器,但也显示它像您一样执行枚举转换并且它有效,您可以将其复制并粘贴到 LinqPad 来运行它。
我们需要有关您的代码的更多信息。
Here is an example that again shows multiple indexers, but also shows it doing the enum conversion like you are doing and it works, you can copy and paste this into LinqPad to run it.
We need more info about your code.