超载索引器不工作

发布于 2024-12-03 03:20:50 字数 2391 浏览 1 评论 0原文

我有一个自定义集合,我想超载索引器。我的问题是,即使签名不同,我也会收到编译时错误。

        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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

真心难拥有 2024-12-10 03:20:50

您绝对可以超载索引器(许多系统类型都会这样做)。这是一个编译没有问题的例子:

using System;

class Foo
{
    public string this[string text]
    {
        get { return text; }
    }

    public int this[int number]
    {
        get { return number; }
    }
}

class FooHolder
{
    public Foo Foo { get; set; }
}

class Test
{
    static void Main()
    {
        var holder = new FooHolder { Foo = new Foo() };

        int x = holder.Foo[10];
        string y = holder.Foo["hello"];
    }
}

看看你是否能想出一个类似的简短但完整的程序,但它失败了,我们可以找出原因。目前存在太多未知因素,无法轻松诊断问题。

You most definitely can overload indexers (and plenty of system types do so). Here's an example which compiles without problem:

using System;

class Foo
{
    public string this[string text]
    {
        get { return text; }
    }

    public int this[int number]
    {
        get { return number; }
    }
}

class FooHolder
{
    public Foo Foo { get; set; }
}

class Test
{
    static void Main()
    {
        var holder = new FooHolder { Foo = new Foo() };

        int x = holder.Foo[10];
        string y = holder.Foo["hello"];
    }
}

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.

望她远 2024-12-10 03:20:50

这是一个示例,再次显示多个索引器,但也显示它像您一样执行枚举转换并且它有效,您可以将其复制并粘贴到 LinqPad 来运行它。

我们需要有关您的代码的更多信息。

public void Main()
{
    DaClass thing = new DaClass();

    Console.WriteLine(thing[1]);
    Console.WriteLine(thing[Magic.Two]);


}

public class DaClass
{
    private List<string> stuff = new List<string>{"Item1", "Item2", "Item3", "Item4"};
    public string this[Magic m]
    {
        get{return stuff[(int)m];}
        set{stuff[(int)m] = value;}
    }

    public string this[int i]
    {
        get{return stuff[i];}
        set{stuff[i] = value;}
    }
}
//Results
//Item2
//Item3

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.

public void Main()
{
    DaClass thing = new DaClass();

    Console.WriteLine(thing[1]);
    Console.WriteLine(thing[Magic.Two]);


}

public class DaClass
{
    private List<string> stuff = new List<string>{"Item1", "Item2", "Item3", "Item4"};
    public string this[Magic m]
    {
        get{return stuff[(int)m];}
        set{stuff[(int)m] = value;}
    }

    public string this[int i]
    {
        get{return stuff[i];}
        set{stuff[i] = value;}
    }
}
//Results
//Item2
//Item3
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文