我试图让我的CompareTo方法,从艺术家类工作,但是它不会从专辑类获得ALBUMNAME。 我敢肯定,它与IComparable的上张专辑类的问题。 请代码具体的答案帮助,三江源。
错误1“Assignment.Album”不实现接口成员“System.IComparable.CompareTo(对象)”
专辑类别:
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; }
}
}
}
艺术家类别:
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
}
}
}