Announcement

Collapse
No announcement yet.

toString überschreiben

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • toString überschreiben

    Hallo

    Ich habe eine Frage: und zwar kann man eine Methode (zum Beispiel toString()) einer Klasse aus einer Property, die auf ein Feld dieser Klasse verweist, überschreiben?
    Um das Jetzt mal zu veranschaulichen ein bischen Code:

    Code:
    pulbic class MyClass
    {
       private bool myBool;
       public bool MyBool
       {
          get{return myBool;}
          set{mybool = value;}
       }
    }

    ich will dass wenn jemand MyClassObject.MyBool.toString() aufruft er nicht die standard toString funktion aurruft sondern meine, und dazu möchte ich nicht eine klasse erstellen die von Bool oder Boolean erbt.

    ich hab mir das ca so vorgestellt:
    Code:
    pulbic class MyClass
    {
       private bool myBool;
       public bool MyBool
       {
          get{return myBool;}
          set{mybool = value;}
          toString{return myBool?"ja":"nein"}
       }
    }


    gibt es etwas in diese richtung?

  • #2
    Hallo,

    ja es muss die Standardmethode überschrieben werden.

    [highlight=c#]
    public class MyClass
    {
    private bool myBool;
    public bool MyBool
    {
    get { return myBool; }
    set { myBool = value; }
    }

    public override string ToString()
    {
    return myBool ? "ja" : "nein";
    }
    }
    [/highlight]

    mfG Gü
    "Any fool can write code that a computer can understand. Good programmers write code that humans can understand". - Martin Fowler

    Comment


    • #3
      ja aber wenn ich es so mache überschreibe ich dieMyClass.toString().
      Ich will aber die MyClass.MyBool.toString() überschreiben.

      Comment


      • #4
        Hallo,

        ein Weg wäre dann über Extension-Methods.

        zB
        [highlight=c#]
        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Text;

        namespace ConsoleApplication1
        {
        public class MyClass
        {
        private bool myBool;
        public bool MyBool
        {
        get { return myBool; }
        set { myBool = value; }
        }

        //public override string ToString()
        //{
        // return myBool ? "ja" : "nein";
        //}
        }

        public static class Extension
        {
        public static string Wert(this bool b)
        {
        return b ? "ja" : "nein";
        }
        }

        class Program
        {
        static void Main(string[] args)
        {
        MyClass mc = new MyClass();
        mc.MyBool = false;

        Console.WriteLine(mc.MyBool.Wert());
        Console.ReadKey();
        }
        }
        }
        [/highlight]

        Hab vorhin "etwas" überlesen.

        mfG Gü
        "Any fool can write code that a computer can understand. Good programmers write code that humans can understand". - Martin Fowler

        Comment


        • #5
          Außerdem von System.Boolean kann nicht geerbt werden, da diese Klasse versiegelt (sealed) ist.
          "Any fool can write code that a computer can understand. Good programmers write code that humans can understand". - Martin Fowler

          Comment


          • #6
            Danke für die Hilfe.
            Mit den Extentions hats funktioniert.

            Jetzt weis ich endlich was die Funktionen mit dem lustigen Blauen Pfeil sind :P

            Comment

            Working...
            X