CompareTo方法不起作用,它不会从Album类 - IComparable问题中得到AlbumName [英] CompareTo method not working, it won't get AlbumName from the Album class- IComparable issue I think

查看:619
本文介绍了CompareTo方法不起作用,它不会从Album类 - IComparable问题中得到AlbumName的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从Artist类获取CompareTo方法,但是它不会从Album类中获取AlbumName。我很确定这是在Album类中的IComparable的问题。请帮助代码特定的答案,谢谢。



错误1'Assignment.Album'不实现接口成员'System.IComparable.CompareTo(object)'



专辑类别:

  using System; 
使用System.Collections.Generic;
使用System.Linq;
使用System.Text;

命名空间赋值
{
class Album:IComparable
{
public string albumName;

public Album(string albumName)//构造函数
{
this.albumName = albumName;

}

public string AlbumName
{
get {return albumName; }
set {albumName = AlbumName; }
}
}
}

艺术家类: p>

  using System; 
使用System.Collections.Generic;
使用System.Linq;
使用System.Text;
命名空间赋值
{
public class Artist:IComparable
{
private String name;
int members = 4; //默认成员数
// string albumName;

LinkedList< Artist> albums = new LinkedList< Artist>();

public override string ToString()//使自己的toString方法
{
return(Name:+ name +Member of Members:+ members);
}

public Artist(string name,int members)//构造函数
{
this.name = name;
this.members = members;
}

public string Name
{
get {return name; }
set {name = Name; }
}

public int成员
{
get {return members; }
set {members = value;


public int CompareTo(object obj)
{
if(obj是Artist)//按名称比较
{
艺术家其他=(艺术家)obj;
return name.CompareTo(other.name); //艺术家名称
}
如果(obj是字符串)//与专辑标题列表进行比较
{
string other = obj.ToString();

if(obj.Equals(name.AlbumName))
return 0;

return -1;
}
else
return -999; //表示不能比较
}

}
}


解决方案

您发布的错误消息说您的Album类中没有Compare(object)方法。你的代码证实了这一点。所以...添加一个!



编辑



好的,让我们分解一下。你显然在学习很多,并尝试了很多新的东西。那很棒。它有助于您有一个牢固的目标和您的工具,所以让我们谈谈这些。



根据您的意见,很明显,您打算使用 IComparable 用于订购和保证表单中的唯一性。通常 IComparable 不用于唯一性,但是您有一个业务需求来使用它,所以...



我们来谈谈手头的工具。我想你只需要了解什么 IComparable 的意思。这意味着实现它的类具有自然的顺序,如列表中的数字或(ahem)名称。具体来说,它保证三个非特殊结果:


  1. 这个是少于参数:返回一些数字小于0

  2. 这个是大于参数:返回一些大于0的数字

  3. 这个与参数的顺序相同:return 0。

就是这样。



使用界面的东西往往是对项目进行排序的集合。他们使用这个数字来指导他们的排序。他们唯一关心的是如果数字小于,大于或等于零。返回号码并不意味着任何其他事情,所以返回 -999 就像你一样不会告诉任何人有用的东西。如果你真正没有能力进行比较,那么正确的做法就是抛出一个例外。



所以希望你有一些想法如何使用IComparable ,我们来讨论一般的实现方式。



你听说过泛型吗?这是.NET的一个基本功能,它将根据类型指导您的代码的编译和执行。如果一个接口是通用的,这意味着你可以多次实现,每次指定一个不同的类型。有一个通用版本的 IComparable System.IComparable< T> 。如果您的课堂需要比较字符串,则应该实现 IComparable< string> 以及 IComparable< Artist> 或 - 具备-你。如果你这样做,你的课程就像这样:

  public class Artist:IComparable< string> IComparable< Artist> 
{
//你的代码

public int比较(string name)
{
//你的代码
}

public int比较(艺术家艺术家)
{
//你的代码
}
}
pre>

如果您正在使用像Windows Forms这样的旧技术,它们期望非通用的 IComparable 接口,那么可以这样实现:

  public int Compare(object obj)
{
if (obj == null)return -1; //也许你想要抛出异常?
if(obj是string)return Compare((string)obj);
if(obj是Artist)return Compare((Artist)obj);

throw new ArgumentException(wtf,unexpected type);
}

最后,为什么您的专辑类中的艺术家名单?想想需要检查唯一性的位置。当用户在表单中输入时,会发生,对吧?你不觉得列表也属于那里吗?


I'm trying to get my CompareTo method to from Artist class to work, however it won't get AlbumName from the Album class. I'm pretty sure it's an issue with IComparable on the Album class. Please help with code specific answers, thankyou.

Error 1 'Assignment.Album' does not implement interface member 'System.IComparable.CompareTo(object)'

Album Class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Assignment
{
    class Album : IComparable
    {
        public string albumName;

        public Album(string albumName)//constructors
        {
            this.albumName = albumName;

        }

        public string AlbumName
        {
            get { return albumName; }
            set { albumName = AlbumName; }
        }
    }
}

Artist Class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Assignment
{
    public class Artist : IComparable
    {
        private String name;
        int members = 4;//default number of members
        //string albumName;

        LinkedList<Artist> albums = new LinkedList<Artist>();

        public override string ToString()//to make my own toString method
        {
            return ("Name: " + name + "Number of Members: " + members);
        }

        public Artist(string name, int members)//constructors
        {
            this.name = name;
            this.members = members;
        }

        public string Name
        {
            get { return name; }
            set { name = Name; }
        }

        public int Members
        {
            get { return members; }
            set { members = value; }
        }

        public int CompareTo(object obj)
        {
            if (obj is Artist) //compare by name
            {
                Artist other = (Artist)obj;
                return name.CompareTo(other.name);//artist name
            }
            if (obj is string) //compare against list of album titles
            {
                string other = obj.ToString();

                    if (obj.Equals(name.AlbumName))
                        return 0;

                return -1;
            }
            else
                return -999;  //indicates can’t make a comparison    
        }

    }
}

解决方案

The error message you post says that there is no Compare(object) method in your Album class. Your code confirms that. So... add one!

EDIT

OK, let's break it down. You're clearly learning a lot, and trying lots of new stuff. That's great. It helps to have a solid idea both of your goals and of your tools, so let's talk about those.

Based on your comments, it's clear that you are intending to use IComparable for ordering and guaranteeing uniqueness in a form. Generally IComparable isn't used for uniqueness, but you have a 'business requirement' to use it, so...

Let's talk about the tools at hand. I think you just need to understand what IComparable means. It means that the class implementing it has a natural ordering, like numbers or (ahem) names in a list. Specifically, it guarantees three non-exceptional outcomes:

  1. this is "less than" the argument: return some number less than 0
  2. this is "greater than" the argument: return some number greater than 0
  3. this has the same order as the argument: return 0.

That's it.

Things that use the interface tend to be collections that sort items. They use that number to guide their sorting. The only thing they care about is if the number is less than, greater than, or equal to zero. The return number doesn't mean anything else, so returning -999 as you do isn't going to tell anybody anything useful. If whatever you have truly is not able to be compared, the right thing to do is to throw an exception.

So hopefully you have some idea how IComparable is used, let's talk about how it's usually implemented.

Have you heard of generics? It's a fundamental feature of .NET that will guide the compilation and execution of your code based on types. If an interface is generic, that means you can implement is multiple times, specifying a different type each time. There is a generic version of IComparable: System.IComparable<T>. If your class needs to compare strings, it should implement IComparable<string> as well as IComparable<Artist> or what-have-you. If you do so, your class will look something like this:

public class Artist : IComparable<string>, IComparable<Artist>
{
    // your code here

    public int Compare(string name)
    {
        // your code here
    }

    public int Compare(Artist artist)
    {
        // your code here
    }
}

If you are working with an older technology like Windows Forms that expects the non-generic IComparable interface, you can implement that, too, like so:

public int Compare(object obj)
{
    if (obj == null) return -1; // maybe you want to throw an exception instead?
    if (obj is string) return Compare((string) obj);
    if (obj is Artist) return Compare((Artist) obj);

    throw new ArgumentException("wtf, unexpected type");
}

Finally, why is your list of artists in the Album class? Think about where the check for uniqueness needs to happen. It happens when the user types in the form, right? Don't you think that the list belongs there, too?

这篇关于CompareTo方法不起作用,它不会从Album类 - IComparable问题中得到AlbumName的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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