Hallo, ich hab ein kleines Programm erst als ConsolenApplication
programmiert und danach zwei Methoden davon in mein WindowsForm Programm
kopiert. Jetzt bekomme ich Fehlermeldungen für die Zeilen.
Und zwar beanstandet er die Zeile mit Convert.xxx
"An object reference is required for the non static field, method, or
poperty 'WindowsFormApplication6'
Wenn ich ein neues WindowsForm Project erstelle, dann kann ich die
Funktionen einfach in das Programm kopieren ohne Probleme. Ich stehe
etwas auf dem Schlau, weil mein Windows Programm schon sehr
fortgeschritten ist und ich nicht nochmal die ganze Form etc. neumachen
möchte
Hm, wenn ich eine neue Class in meiner WindowsForm erstelle und den Code
in diese Class kopiere bekomme ich keine Fehler. Nur wenn ich es in
meine Form1.cs kopiere. Kann mir das jemand erklären?
Thorsten schrieb:> Und zwar beanstandet er die Zeile mit Convert.xxx> "An object reference is required for the non static field, method, or> poperty 'WindowsFormApplication6'
die Methode ist static kann damit nicht auf Klassenvariabel zugreifen.
Vermutich ist "reports" so eine.
Reports ist ein Anoymnus object. Ich muss die Methode nochmal
umschreiben, weil ich die das Obj.x und Obj.y als Klasse benötige zur
Übergabe an eine andere Methode.
Ich kämpfe damit noch ein bisschen über select new in eine Klasse zu
schreiben.
Deine Beschreibung ist nicht sehr aussagekräftig. Der Compiler sagt dir
doch genau die Zeile, die er nicht mag - nicht nur "irgendwo bei
Convert.xxx".
Jedenfalls versuchst du in betreffenden Zeile auf ein "field, method, or
poperty" zuzugreifen, das "WindowsFormApplication6" heißt. Das ist schon
einmal merkwürdig, weil du ja eine "ConsolenApplication" angelegt hast.
Wo ist denn überhaupt die Methode "read" definiert? Ist die denn auch
als static deklariert?
Ja, war auch als Static deklaiert.
Ich hab es jetzt anders gelöst vorallendingen weil ich die Methoden
sowieso in eine andere Klasse auslagern möchte, damit das Programm
leichter adaptierbar bleibt.
// double x = Convert.ToDouble(obj.x, CultureInfo.InvariantCulture);
32
// convert to y double with cultureinfo
33
// double y = Convert.ToDouble(obj.y, CultureInfo.InvariantCulture);
34
#if DEBUG
35
Debug.WriteLine("Start X : {0} - Start Y : {1}",Coordinates.x,Coordinates.y);
36
#endif
37
}
38
39
}
Jetzt kann ich auf die Methode von meiner WindowsForm zugreifen.
Testcases.enter(.....);
Wie ich nun auf die Rückgabewerte zugreife von meiner Enter Methode
bleibt mir noch ein Rätsel.
Console.Writeln("{0}, {1}", Coordinates.x, Coorndinates.y) klappt nicht
so ganz in meiner WindowsForm Class.
Habt Ihr ein Tipp?
Thorsten schrieb:> Oder gibt es noch Verbesserungen?
ja, auf das ganze static verzichten.
Man schreibt sich eine eigene Klasse mit seiner gekapselten
Funktionalität. Dies kann man dann in einen Konsolenprogram als static
objekt anlegen oder bei winforms einfach so nutzen.
Das ganze Static mach sonst früher oder später großen Ärgern.