亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

無法將類型“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條經驗 獲得超5個贊

是否有某種原因導致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 關注
  • 158 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號