为了账号安全,请及时绑定邮箱和手机立即绑定

无法将类型“string”隐式转换为“XXX.ClassName”

无法将类型“string”隐式转换为“XXX.ClassName”

C#
白衣非少年 2022-12-24 10:08:26
我创建了一个名为 Student 的类,Program.cs 中有以下代码:public static IList<Student> Students { get; private set; }private static void AddStudent()    {        /*try        {            Console.Write("First name: ");            //Students.FirstName = Console.ReadLine();            string firstName = Console.ReadLine();         }        catch (ArgumentNullException)        {            Console.WriteLine("Name was left empty.");        }*/        Console.Write("First name: ");        string firstName = Console.ReadLine();        Console.Write("Last name: ");        string lastName = Console.ReadLine();                    var newStudent = new Student {FirstName = firstName, LastName = lastName}; //if I use try-catch block, it says: The name 'firstName' doesn't exist in the current context        Students.Add(newStudent);        Console.WriteLine("The new student is added. \nEnter any key to return to main screen.");        Console.ReadKey();}public static void SortStudents(IList<Student> students)    {        string temp;        for (int i = 0; i < students.Count; i++)        {            for (int j = 0; j < students.Count; j++)            {                if (string.Compare(students[i].ToString(), students[j].ToString()) < 0)                {                    //swap                    temp = students[i].ToString();                    students[i] = students[j];                    students[j] = temp; //error here                }            }        }        Console.WriteLine(students);    }学生班:public class Student{    public string FirstName { get; set; }    public string LastName { get; set; }    public int Age { get; set; }    public string StudentNumber { get; set; }    public string Gender { get; set; }    public string FieldOfStudy { get; set; }}我正在尝试实现一种算法,该算法将按字母顺序对输入的名称进行排序,但它会在那里抛出错误。我该如何解决?我也在尝试使用 try-catch 块,但它抛出了我在代码中评论过的错误。提前致谢!
查看完整描述

2 回答

?
忽然笑

TA贡献1806条经验 获得超5个赞

正如一些评论员指出的那样,您可能需要访问Student和比较它们的属性,然后交换对象。


所以像这样:


public static void SortStudents(IList<Student> students)

    {

        //We change this to Type Student, not string.

        Student temp;

        for (int i = 0; i < students.Count; i++)

        {

            for (int j = 0; j < students.Count; j++)

            {

                //We look at the Properties of the object, not the Object.ToString()  

                if (string.Compare(students[i].FirstName, students[j].FirstName) < 0)

                {

                    //Here we are swapping the objects, because we have determined 

                    //Their first names aren't in alphabetical order.  

                    temp = students[i];

                    students[i] = students[j];

                    students[j] = temp;

                }

            }

        }

        //For loop, or Foreach loop here to iterate through your collection (ILIST)

    }


查看完整回答
反对 回复 2022-12-24
?
收到一只叮咚

TA贡献1821条经验 获得超4个赞

是否有某种原因导致Student该类无法通过实现IComparable接口来实现这种“排序”逻辑?使用 aList<Student>来保存Student对象将使用此CompareTo方法对对象进行“排序”。这将允许类以任何你想要的方式“排序”。在这种情况下,它按姓氏然后按名字排序。你试过这样做吗?它可能看起来像下面...


public class Student : IComparable {

  public string FirstName { get; set; }

  public string LastName { get; set; }

  public int Age { get; set; }

  public string StudentNumber { get; set; }

  public string Gender { get; set; }

  public string FieldOfStudy { get; set; }


  public int CompareTo(object obj) {

    Student that = (Student)obj;

    if (this.LastName.Equals(that.LastName))

      return this.FirstName.CompareTo(that.FirstName);

    return this.LastName.CompareTo(that.LastName);

  }

}

然后,“排序” aList<Student>将只是......


Students.Sort();


查看完整回答
反对 回复 2022-12-24
  • 2 回答
  • 0 关注
  • 108 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信