Autor Beitrag
Sharpener
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 24

Win 95, Win 98, Win 2000, Win XP, Win Vista, Win 7, Win 8, Mac OS X 10.4, Mac OS X 10.7, Mac OS X 10.8
C# (VS 2012), SQL, PHP, HTML&CSS,Java
BeitragVerfasst: Sa 22.10.11 13:41 
Hi,
ich arbeite seit geraumer Zeit ein Lehrbuch zu C# durch , dort wird nun an einem Klassenprojekt gearbeitet ,doch (der Code im IDE ist gleich dem im Buch) bekomme ich Fehler ,die ich nicht zu lösen weiß :S
Circle.cs
ausblenden volle Höhe C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GeometricObjects
{
    public class Circle
    {
        public int XCoordinate;
        public int YCoordinate;
        public double Radius;

        //Felder
        private double _Radius;
        private int _XCoordiante;
        private int _YCoordiante;

        //Eigenschaftsmethoden
        public int YCoordinate
        {
            get { return _YCoordiante; }
            set { _YCoordiante = value; }
        }

        public int XCoordinate
        {
            get { return _XCoordiante; }
            set { _XCoordiante = value; }
        }
        public double Radius
        {
            get { return _Radius; }
            set
            {
                if (value >= 0)
                    _Radius = value;
                else
                    Console.WriteLine("Unzulässiger negativer Radius.");
            }
        }
        
        //Methoden
        public double GetArea()
        {
            double area = 3.14 * Math.Pow(Radius, 2);
            return area;
        }

        public double GetCircumference()  {
            double circumferne = 2 * 3.14 * Radius;
            return circumferne;
        }


    }
}

Programm.cs
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GeometricObjects
{
    class Program
    {
        static void Main(string[] args)
        {
            Circle kreis = new Circle() { Radius = 12 };
            double _area = kreis.GetArea();
            Console.WriteLine(" Fläche = {0}", _area);
            Console.WriteLine(" Umfang = {0}", kreis.GetCircumference());


            Console.ReadLine();
        }
    }
}


Nach Buch sollte das Klappen aber (!) bei mir halt nicht und Tippfehler sind keine drin.
FehlerCode:
ausblenden Quelltext
1:
2:
3:
4:
5:
Fehler  1  Der Typ "GeometricObjects.Circle" enthält bereits eine Definition für "YCoordinate".  I:\C Sharp Projekte\GeometricObjectsSolution\GeometricObjects\Circle.cs  20  20  GeometricObjects

Fehler  2  Der Typ "GeometricObjects.Circle" enthält bereits eine Definition für "XCoordinate".  I:\C Sharp Projekte\GeometricObjectsSolution\GeometricObjects\Circle.cs  26  20  GeometricObjects

Fehler  3  Der Typ "GeometricObjects.Circle" enthält bereits eine Definition für "Radius".  I:\C Sharp Projekte\GeometricObjectsSolution\GeometricObjects\Circle.cs  31  23  GeometricObjects


Was ist falsch gelaufen?
Ausserdem erhalte ich zahlreiche "Ähnlichkeits und Mehrdeutigkeitsfehler" die aber beim Debuggen verschwinden.
Ich bin kein totaler Anfänger habe schon vorher programmiert aber nicht Objektorientiert.
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4796
Erhaltene Danke: 1059

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: Sa 22.10.11 13:55 
Hallo,

die ersten drei Member
ausblenden C#-Quelltext
1:
2:
3:
public int XCoordinate;
public int YCoordinate;
public double Radius;

hast du ja nochmal in deiner Klasse als Eigenschaften (Properties) angelegt.
Ich nehme mal an, daß im Buch steht, daß du statt der öffentlichen Felder jetzt besser Eigenschaften dafür erstellen sollst (und dann dafür die public Member löschen sollst)?
Um welches Buch handelt es sich denn?

P.S. bei
ausblenden C#-Quelltext
1:
2:
private int _XCoordiante;
private int _YCoordiante;

hast du noch Rechtschreibfehler ;-)
Sharpener Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 24

Win 95, Win 98, Win 2000, Win XP, Win Vista, Win 7, Win 8, Mac OS X 10.4, Mac OS X 10.7, Mac OS X 10.8
C# (VS 2012), SQL, PHP, HTML&CSS,Java
BeitragVerfasst: Sa 22.10.11 15:15 
So nach entfernen der Rechtschreibfehler kommt nurnoch ein Compiler Fehler
ausblenden Quelltext
1:
Fehler  1  Der Typ "GeometricObjects.Circle" enthält bereits eine Definition für "Radius".  I:\C Sharp Projekte\GeometricObjectsSolution\GeometricObjects\Circle.cs  31  23  GeometricObjects					

Bei dem Buch handelt es sich um "Visual C# 2010" von Andreas Kühnel ist von Galileo Computing. Ich hab es als Buchform, ist aber auch als OpenBook verfügbar.
Zurzeit bin ich bei ... openbook.galileocomp...87840d99873a40e07b4b !

Die ersten 3 Member hab ich nach Buch nochmal _Member angelegt ,um per get und Set diese zu setzten und Radius näher zu bestimmen (nicht unter 0).
Die public Member sollte ich nicht löschen (?).
Ich schau nochmal drüber... oder du auch per OpenBook :D

Sharpener
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Sa 22.10.11 15:28 
Die öffentlichen Felder müssen weg, die hast Du ja durch die gleichnamigen Eigenschaften ersetzt. Keine Ahnung, ob das im Buch vielleicht nicht so deutlich geschrieben wird, aber wenn die weg sind, sollte es passen ;-)

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
Sharpener Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 24

Win 95, Win 98, Win 2000, Win XP, Win Vista, Win 7, Win 8, Mac OS X 10.4, Mac OS X 10.7, Mac OS X 10.8
C# (VS 2012), SQL, PHP, HTML&CSS,Java
BeitragVerfasst: Sa 22.10.11 15:35 
Also aus dem Quellcode müssen raus...
ausblenden C#-Quelltext
1:
2:
3:
        private double Radius;
        private int XCoordiante;
        private int YCoordiante;

?
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Sa 22.10.11 15:38 
Äh, wieso sind die jetzt private? :gruebel:

Du solltest im Quellcode haben: Private Felder mit "_" davor und öffentliche Eigenschaften. Keine öffentlichen Felder.

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
Sharpener Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 24

Win 95, Win 98, Win 2000, Win XP, Win Vista, Win 7, Win 8, Mac OS X 10.4, Mac OS X 10.7, Mac OS X 10.8
C# (VS 2012), SQL, PHP, HTML&CSS,Java
BeitragVerfasst: Sa 22.10.11 15:44 
oh Sorry das war ein Kopierfehler :oops:
ausblenden C#-Quelltext
1:
2:
3:
4:
 //Felder
        private double _Radius;
        private int _XCoordiante;
        private int _YCoordiante;

private felder raus und die public da lassen.
Sinn der Sache war es die Datenkapselung zu erlernen
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Sa 22.10.11 15:50 
user profile iconSharpener hat folgendes geschrieben Zum zitierten Posting springen:
private felder raus und die public da lassen.
Lies nochmal, was ich geschrieben habe ;)

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
Ralf Jansen
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 4708
Erhaltene Danke: 991


VS2010 Pro, VS2012 Pro, VS2013 Pro, VS2015 Pro, Delphi 7 Pro
BeitragVerfasst: Sa 22.10.11 15:51 
Zitat:
oh Sorry das war ein Kopierfehler
....
private felder raus und die public da lassen.


Und das ein Denkfehler. Anders rum.
Sharpener Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 24

Win 95, Win 98, Win 2000, Win XP, Win Vista, Win 7, Win 8, Mac OS X 10.4, Mac OS X 10.7, Mac OS X 10.8
C# (VS 2012), SQL, PHP, HTML&CSS,Java
BeitragVerfasst: Sa 22.10.11 16:20 
Ahhhso verstanden ;)
jetzt funktioniert das Programm ohne Probleme und gibt die richtigen Werte aus
Th69
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Moderator
Beiträge: 4796
Erhaltene Danke: 1059

Win10
C#, C++ (VS 2017/19/22)
BeitragVerfasst: Sa 22.10.11 20:32 
Hallo Sharpener,

in dem Quelltext unter 3.5.3 Ergänzung der Klasse »Circle« sind aber die öffentlichen Felder nicht mehr aufgelistet (es ist ja explizit von "ersetzen" die Rede).
Sharpener Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 24

Win 95, Win 98, Win 2000, Win XP, Win Vista, Win 7, Win 8, Mac OS X 10.4, Mac OS X 10.7, Mac OS X 10.8
C# (VS 2012), SQL, PHP, HTML&CSS,Java
BeitragVerfasst: So 23.10.11 13:45 
Errare humanum est...
Ist mir erst jetzt wirklich aufgefallen, wohl zu schnell gelesen