C# Problem mit Mehtod in WindowsForm

Gast #4416991
Lesenswert?

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.
1
       static void enter(string path, string filename, string id, string name, string start_X, string start_Y)
2
        {
3

4
            // Read xml file to xdoc
5
            XDocument xdoc = XDocument.Load(path + filename);
6
            // Query X and Y Start Position
7
            var reports = from report in xdoc.Descendants("Table1")
8
                          where report.Element(id).Value.Contains(name)
9
                          select new
10
                          {
11
                              x = report.Element(start_X).Value,
12
                              y = report.Element(start_Y).Value
13
                          };
14
            // for all values
15
            foreach (var obj in reports)
16
            {
17

18
                // convert x to double with cultureinfo
19
                double x = Convert.ToDouble(obj.x, CultureInfo.InvariantCulture);
20
                // convert to y double with cultureinfo
21
                double y = Convert.ToDouble(obj.y, CultureInfo.InvariantCulture);
22
#if DEBUG
23
                Debug.WriteLine("Start X : {0} - Start Y : {1}", x, y);
24
#endif
25
                //Console.WriteLine("X : {0} - Y : {1}", Convert.ToDouble(obj.x, CultureInfo.InvariantCulture), Convert.ToString(obj.y, CultureInfo.InvariantCulture));
26
                read(Convert.ToString(x, CultureInfo.InvariantCulture));
27
                read(Convert.ToString(y, CultureInfo.InvariantCulture));
28
            }
29

30
        }

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
Gast #4416997
Lesenswert?

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?
Gast #4416998
Lesenswert?

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.
Gast #4417008
Lesenswert?

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.
Gast #4417033
Lesenswert?

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?
Gast #4417044
Lesenswert?

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.
1
    public class Coordinates
2
    {
3
        public double x { get; set; }
4
        public double y { get; set; }
5
    }
6
    
7
    
8
    public static class Testcases
9
    {
10
       
11
        public static void enter(string path, string filename, string id, string name, string start_X, string start_Y)
12
        {
13

14
            // Read xml file to xdoc
15
            XDocument xdoc = XDocument.Load(path + filename);
16
            // Query X and Y Start Position
17
            var reports = from report in xdoc.Descendants("Table1")
18
                          where report.Element(id).Value.Contains(name)
19
                          select new Coordinates
20
                          {
21
                              x = Convert.ToDouble(report.Element(start_X).Value, CultureInfo.InvariantCulture),
22
                              y = Convert.ToDouble(report.Element(start_Y).Value, CultureInfo.InvariantCulture)
23
                          };
24
            
25
            
26
            // for all values
27
            foreach (var Coordinates in reports)
28
            {
29

30
                // convert x to double with cultureinfo
31
//                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?
Gast #4417182
Lesenswert?

Hat etwas gedauert, aber nun funktioniert es wie es soll.
1
 public class Coordinates
2
    {
3
        public double x { get; set; }
4
        public double y { get; set; }
5
    }
6
    
7
    
8
    public static class Testcases
9
    {
10
       
11
        public static List<Coordinates> GetNodeData(string path, string filename, string id, string name, string start_X, string start_Y)
12
        {
13

14
            // Read xml file to xdoc
15
            XDocument xdoc = XDocument.Load(path + filename);
16
            // Query X and Y Start Position
17
            return (from report in xdoc.Descendants("Table1")
18
                          where report.Element(id).Value.Contains(name)
19
                          select new Coordinates
20
                          {
21
                              x = Convert.ToDouble(report.Element(start_X).Value, CultureInfo.InvariantCulture),
22
                              y = Convert.ToDouble(report.Element(start_Y).Value, CultureInfo.InvariantCulture)
23
                          }).ToList();
24
            }
25

26
        }

und dann in meiner Main
1
const string pfad = @"C:\";
2
            const string datei = "test.xml";
3
            List<Coordinates> results = new List<Coordinates>();
4
            results = Testcases.GetNodeData(pfad, datei, "ID_of_Route", "50147", "Start_X", "Start_Y");
5
            //Debug.WriteLine("X:{0} Y:{1}", results., results[1]);
6
            foreach (Coordinates c in results)
7
            {
8
                Debug.WriteLine("X:{0},Y:{1}", c.x, c.y);
9
            }

Oder gibt es noch Verbesserungen?
Gast #4417196
Lesenswert?

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.
Gast #4417222
Lesenswert?

Ich hab es umgearbeitet, soweit ok?
1
 const string pfad = @"C:\";
2
            const string datei = "test.xml";
3
            List<Coordinates> testcase_coordinates = new List<Coordinates>();
4
            List<Coordinates> headunit_coordinates = new List<Coordinates>();
5
            Testcases testcase = new Testcases();
6
            testcase_coordinates = testcase.GetNodeData(pfad, datei, "ID_of_Route", "50147", "Start_X", "Start_Y");
7
            //Debug.WriteLine("X:{0} Y:{1}", results., results[1]);
8
            foreach (Coordinates c in testcase_coordinates)
9
            {
10
                Debug.WriteLine("X:{0},Y:{1}", c.x, c.y);
11
            }
1
public class Coordinates
2
    {
3
        public double x { get; set; }
4
        public double y { get; set; }
5
    }
6
    
7
    
8
    public class Testcases
9
    {
10
       
11
        public List<Coordinates> GetNodeData(string path, string filename, string id, string value, string X, string Y)
12
        {
13

14
            // Read xml file to xdoc
15
            XDocument xdoc = XDocument.Load(path + filename);
16
            // Query X and Y Start Position
17
            return (from report in xdoc.Descendants("Table1")
18
                          where report.Element(id).Value.Contains(value)
19
                          select new Coordinates
20
                          {
21
                              x = Convert.ToDouble(report.Element(X).Value, CultureInfo.InvariantCulture),
22
                              y = Convert.ToDouble(report.Element(Y).Value, CultureInfo.InvariantCulture)
23
                          }).ToList();
24
            }
25

26
        }

Antwort schreiben

Bitte melde dich an, um einen Beitrag zu schreiben.

oder

Mit Google-Account einloggen

Die Registrierung ist kostenlos und dauert nur eine Minute.

Jetzt registrieren