简体   繁体   中英

Public override string more returns

School asks me to use the public override string. I would like to have something like this:

lbl_Name.Text = ToString(*Field: Naam from class Gebruikerklasse*)
lbl_Surname.Text = ToString(*Field: Achternaam from class Gebruikersklasse*)

I have multiple fields in my class, but I want to return just a few of them. Do I need different methods or can I do it with just one method and more returns with some if statements and booleans?

This is what I have now:

 using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace BurnThatFat
    {
        class Gebruikerklasse
        {
            public string Naam;
            public string Achternaam;
            public int Leeftijd;
            public string Geslacht;
            public int Huidiggewicht;
            public int Streefgewicht;
            public string Gebruikersnaam;
            public string Wachtwoord;

            public override string ToString()
            {
                return Naam;

            }
// I want to use the same method again but this time for another field.


 public override string ToString()
        {
            return Gebruikersnaam;
        }

    }

}
using System;

namespace BurnThatFat
{
    class Program
    {
        static void Main()
        {
            var g1 = new Gebruikerklasse1() { Naam = "Duncan", Geslacht = "Male", Huidiggewicht = 75, Gebruikersnaam = "DCarr" };
            var g2 = new Gebruikerklasse2() { Naam = "Duncan", Geslacht = "Male", Huidiggewicht = 75, Gebruikersnaam = "DCarr" };

            Console.WriteLine(g1);
            Console.WriteLine(g2);

            Console.ReadKey();
        }
    }

    class Gebruikerklasse1
    {
        public string Naam;
        public string Achternaam;
        public int Leeftijd;
        public string Geslacht;
        public int Huidiggewicht;
        public int Streefgewicht;
        public string Gebruikersnaam;
        public string Wachtwoord;
    }

    class Gebruikerklasse2
    {
        public string Naam;
        public string Achternaam;
        public int Leeftijd;
        public string Geslacht;
        public int Huidiggewicht;
        public int Streefgewicht;
        public string Gebruikersnaam;
        public string Wachtwoord;

        public override string ToString()
        {
            return string.Format("{0} - {1} - {2} - {3}", Gebruikersnaam, Geslacht, Huidiggewicht, Naam);
        }
    }
}

Output is:

BurnThatFat.Gebruikerklasse1
DCarr - Male - 75 - Duncan

ie

"g1" outputs default class-to-string output
"g2" outputs overriden format - which can include any members you like

Sorry if I have misunderstood.

I suggest implementing IFormattable interface:

    class Gebruikerklasse {
      ...

      // "A" - Achternaam
      // "G" - Gebruikersnaam
      // "N" - Naam
      // null, empty - default ToString format  
      public string ToString(string format, IFormatProvider formatProvider) {
        if (string.IsNullOrEmpty(format))
          return ToString(); 
        else if ("N".Equals(format, StringComparison.OrdinalIgnoreCase))
          return Naam;
        else if ("A".Equals(format, StringComparison.OrdinalIgnoreCase))
          return Achternaam;
        else if ("G".Equals(format, StringComparison.OrdinalIgnoreCase))
          return Gebruikersnaam;
        else
          throw new FormatException($"Unknown format '{format}'");
      }

      public string ToString(string format) {
        return ToString(format, CultureInfo.CurrentCulture);
      }

      public override string ToString() {
        return Gebruikersnaam;
      }
    }

And so you can put:

    Gebruikerklasse instance = new Gebruikerklasse();

    lbl_Name.Text = instance.ToString("G");
    lbl_Surname.Text = instance.ToString("A");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM