2016-07-31 3 views
0

angezeigt. Ich folgte dem Lernprogramm unter youtube. Um 8:04 Uhr hat der Lehrer die lokale Methode an die überladene ToString angehängt. Wenn ich das versuche, sehe ich die überladene Methode in Intellisense nicht.Overloaded ToString wird nicht in IntelliSense

enter image description here

public class Person 
    { 
     public string FirstName { get; set; } 
     public string LastName { get; set; } 
     public DateTime StartDate { get; set; } 
     public int Rating { get; set; } 

     public override string ToString() 
     { 
      return string.Format("{0} {1}", FirstName, LastName); 
     } 

     public string ToString(PersonFormat format) 
     { 
      return format(this); 
     } 

Ich versuchte auch das Schlüsselwort override Hinzufügen es nicht work.Looks wie ich etwas irgendwo bin fehlt.

Antwort

0

Dies liegt daran, dass var den aktuellen Typ des Objektes versteckt, wenn es über PersonListBox.Items aufgerufen wird. Die folgenden Werke:

foreach(Person person in PersonListBox.Items) 
{ 


} 

While:

foreach(var person in PersonListBox.Items) 
{ 
    // person is object. It doesn't have the method you want to call. 
    // it even doesn't have FirstName or LastName 
    // try person. and you can clearly see it only has the common 
    // object methods. 
} 

Hinweis: Sie auch foreach(var p in PersonListBox.Items.OfType<Person>())

+0

Dank verwenden können! Es funktioniert ! – Noob

+0

@ user3185569: Akzeptiert! Danke vielmals ! – Noob

Verwandte Themen