C# nie może przekonwertować metody na typ non delegate
Mam klasę o nazwie Pin
.
public class Pin
{
private string title;
public Pin() { }
public setTitle(string title) {
this.title = title;
}
public String getTitle()
{
return title;
}
}
Z innej klasy dodaję piny obiekty w pinach List<Pin>
, a z innej chcę iterację pinów listy i pobierać elementy. Więc mam ten kod.
foreach (Pin obj in ClassListPin.pins)
{
string t = obj.getTitle;
}
Tym kodem nie mogę odzyskać tytułu. Dlaczego?
(Uwaga: ClassListPin
jest tylko klasą statyczną, która zawiera pewne elementy, a jednym z nich jest Pin List<Pin>
)
7 answers
Musisz dodać nawiasy po wywołaniu metody, w przeciwnym razie kompilator pomyśli, że mówisz o samej metodzie( typie delegata), podczas gdy ty mówisz o zwracanej wartości tej metody.
string t = obj.getTitle();
Dodatkowe Informacje Nieistotne
Również, spójrz na właściwości. W ten sposób możesz używać title tak, jakby była zmienną, podczas gdy wewnętrznie działa jak funkcja. W ten sposób nie musisz pisać funkcji getTitle()
i setTitle(string value)
, ale można to zrobić tak:
public string Title // Note: public fields, methods and properties use PascalCasing
{
get // This replaces your getTitle method
{
return _title; // Where _title is a field somewhere
}
set // And this replaces your setTitle method
{
_title = value; // value behaves like a method parameter
}
}
Lub możesz użyć automatycznie zaimplementowanych właściwości, które domyślnie używają tego:
public string Title { get; set; }
I nie musiałbyś tworzyć własnego zaplecza (_title
), kompilator sam by je stworzył.
Możesz również zmienić poziomy dostępu dla accessorów właściwości (getterów i setterów):
public string Title { get; private set; }
Używasz właściwości tak, jakby były polami, np.:
this.Title = "Example";
string local = this.Title;
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-02-14 15:03:22
getTitle
jest funkcją, więc po niej należy umieścić ()
.
string t = obj.getTitle();
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-02-14 14:47:30
Jak stwierdził @ Antonijn, musisz wykonać metodę getTitle, dodając nawiasy:
string t = obj.getTitle();
Ale chcę dodać, że zajmujesz się programowaniem w Javie w C#. Istnieje pojęcie Właściwości (para metod get I set), które należy stosować w takich przypadkach:
public class Pin
{
private string _title;
// you don't need to define empty constructor
// public Pin() { }
public string Title
{
get { return _title; }
set { _title = value; }
}
}
I jeszcze więcej, w tym przypadku możesz poprosić kompilator nie tylko o generowanie metod get I set, ale także o generowanie back storage, poprzez właściwość auto-impelemented sposób użycia:
public class Pin
{
public string Title { get; set; }
}
I teraz nie musisz wykonywać metody, ponieważ właściwości używane jak Pola:
foreach (Pin obj in ClassListPin.pins)
{
string t = obj.Title;
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-02-14 14:51:40
Jak wspomniano, należy użyć obj.getTile()
Ale w tym przypadku myślę, że chcesz użyć Właściwości .
public class Pin
{
private string title;
public Pin() { }
public setTitle(string title) {
this.title = title;
}
public String Title
{
get { return title; }
}
}
To pozwoli Ci używać
foreach (Pin obj in ClassListPin.pins)
{
string t = obj.Title;
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-02-14 15:30:51
Możesz uprościć kod swojej klasy do tego poniżej i będzie działał tak, jak jest, ale jeśli chcesz, aby twój przykład działał, dodaj nawias na końcu: string x = getTitle ();
public class Pin
{
public string Title { get; set;}
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-02-14 14:53:48
Ponieważ getTitle
nie jest string
, zwraca referencję lub delegate
do metody (jeśli chcesz), jeśli nie jawnie wywoła metodę.
Wywołaj swoją metodę w ten sposób:
string t= obj.getTitle() ; //obj.getTitle() says return the title string object
To jednak zadziała:
Func<string> method = obj.getTitle; // this compiles to a delegate and points to the method
string s = method();//call the delegate or using this syntax `method.Invoke();`
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-02-14 14:52:03
Aby wykonać metodę należy dodać nawiasy, nawet jeśli metoda nie przyjmuje argumentów.
Tak powinno być:
string t = obj.getTitle();
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-02-14 14:47:30