Autor Beitrag
erfahrener Neuling
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 233
Erhaltene Danke: 19

Win 7, Win 10
C#, ASP-MVC (VS 2017 Community), MS SQL, Firebird SQL
BeitragVerfasst: Mi 20.04.16 13:50 
Hallo,

ich möchte eine Eigenschaft nach folgender Definition haben:
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
class Beipiel
{
    public class Get
    {
        public static string HelloWorld
        {
            get { return "Hello World!"; }
        }
    }
}

Meine Frage: Kann ich solche Eigenschaften auch nicht static definieren? Und wenn nein, wieso nicht? Eine 'einfache' Eigenschaft muss ja auch nicht static sein (zum Beipsiel.HelloWorld).
Danke mal wieder im Vorraus ;)
Ralf Jansen
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 4700
Erhaltene Danke: 991


VS2010 Pro, VS2012 Pro, VS2013 Pro, VS2015 Pro, Delphi 7 Pro
BeitragVerfasst: Mi 20.04.16 13:54 
Einfach das Schlüsselwort static weg lassen.

Du brauchst dann aber natürlich eine Instanz der Klasse Get (übrigens doofer Name).

ausblenden C#-Quelltext
1:
2:
var instance = new Beispiel.Get();
var hello = instance.HelloWorld;
erfahrener Neuling Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 233
Erhaltene Danke: 19

Win 7, Win 10
C#, ASP-MVC (VS 2017 Community), MS SQL, Firebird SQL
BeitragVerfasst: Mi 20.04.16 13:58 
Achso natürlich! Hab vergessen, eine zu erstellen und die Eigenschaft static-mäßig aufgerufen :autsch:
Trotzdem danke!