Autor Beitrag
ThomAlex
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 58

Windows 7
C# (VS08) Games via XNA (privat) oder WinForms (Schule)
BeitragVerfasst: Mo 19.10.09 18:34 
Hallo ich habe vor ein paar Wochen angefangen mich mit Programmierung zu beschäftigen, ich bin kein Umsteiger sondern Neuling in dieser Materie. Mit der Programmiersprache C# arbeite ich und komme bei einem Thema nicht zurecht.
Nämlich befasse ich mich gerade mit Eigenschaftsmethoden und da kamen die Accessoren "set" und "get" zu Wort. Quelle: Galileo Computing C# Tutorial

ausblenden 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:
class Program
    {
        static void Main(string[] args)
        {
            Circle neu = new Circle();
            Console.WriteLine(neu.Radius);
            Console.ReadLine();
        }
    }

public class Circle
    {
        private double radius = -1;

        // Eigenschaftsmethode 
        public double Radius
        {
            get
            {
                return radius;
            }
            set
            {
                if (value >= 0)
                    radius = value;
                else
                    Console.Write("Unzulässiger Wert.");
            }
        }
    }

ich will das auslesen und hab auch "radius" verschiedene Zahlen zugewiesen und bei ungültigen Zahlen sollte die Ausgabe: Unzulässiger Wert, erscheinen, das ist aber nicht der Fall, wo ist mein Denkfehler,
Danke im vorraus für hilfreiche Antworten
MFG ThomAlex

Moderiert von user profile iconChristian S.: C#-Tags hinzugefügt
JasonDelife
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 81

Windows 7 Professional
C# (Visual Studio 2008 Professional), Java (NetBeans IDE 6.7)
BeitragVerfasst: Mo 19.10.09 20:28 
Radius wird ja nichts zugeweisen.
Du erzeugst einen neuen Kreis.
Dann gibst du den Radius (-1) aus.
Ende.
Der set-Teil wird hier garnicht benutzt.

Grüße, JasonDelife.
ThomAlex Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 58

Windows 7
C# (VS08) Games via XNA (privat) oder WinForms (Schule)
BeitragVerfasst: Mo 19.10.09 21:00 
ok das stimmt, es ist bei solch Code für Anfänger schwer den Überblick zu behalten xD
Könntest du mir ein ganz simples Beispiel (vllt auch sogar auf meins bezogen) coden, wo set zum Einsatz kommt? Würde es dann besser nachvollziehen können
MFG
Gabe
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 39



BeitragVerfasst: Mo 19.10.09 21:02 
user profile iconThomAlex hat folgendes geschrieben Zum zitierten Posting springen:
ok das stimmt, es ist bei solch Code für Anfänger schwer den Überblick zu behalten xD
Könntest du mir ein ganz simples Beispiel (vllt auch sogar auf meins bezogen) coden, wo set zum Einsatz kommt? Würde es dann besser nachvollziehen können
MFG


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:
class Program
    {
        static void Main(string[] args)
        {
            Circle neu = new Circle();
            Console.WriteLine("Radius: ");
            neu.Radius  = Convert.ToInt32(Console.ReadLine());
            Console.ReadLine();
        }
    }

    public class Circle
    {
        private double radius;

        // Eigenschaftsmethode
        public double Radius
        {
            get
            {
                return radius;
            }
            set
            {
                if (value >= 0)
                    radius = value;
                else
                    Console.Write("Unzulässiger Wert.");
            }
        }
    }


Hi,
dein Beispiel etwas abgeändert. So funktioniert es wie du willst.

Tipp:
Haltepunkte setzen und durchdebuggen, dann siehst du schön wie der setter aufgerufen wird.

Gruß Gabe
ThomAlex Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 58

Windows 7
C# (VS08) Games via XNA (privat) oder WinForms (Schule)
BeitragVerfasst: Mo 19.10.09 21:21 
Danke sehr hilfreich, echt nett
Damit wär mein Problem gelöst