简体   繁体   English

C#通过其他类中的ToString调用类中的ToString

[英]C# Calling ToString in class by ToString in other class

I have 2 classes. 我有2节课。 My question is, how can I call ToString from first class called Racer in my second class called Time. 我的问题是,如何在我的第二个班级“时间”中从称为“赛车手”的第一班级调用ToString。

Simplified version: class B To string (return class A ToString + something from class B) 简化版本:B类到字符串(返回A类ToString + B类中的内容)

class Racer
{
    public string name, surname;


    public void ReadingSeparatorsRacer(string line) //Rozdělení separatorem
    {

        char[] separators = new char[] { ';' };
        string[] field = line.Split(separators, StringSplitOptions.RemoveEmptyEntries);

        surname = field[0]; //Příjmení
        name = field[1]; //Jméno
    }

    public override string ToString()
    {
        return surname + name;
    }           
}

class Time
{
    DateTime startTime, finishTime, result;

    public void ReadingSeparatorsTime(string line)
    {
        char[] separators = new char[] { ';', ':', '.' };
        string[] field = line.Split(separators, StringSplitOptions.RemoveEmptyEntries);
    }

    public override string ToString()
    {
        string s = Racer.ToString

        return "" result;

    }
} 

Iam thinking about something like this: 我正在考虑这样的事情:

public override ToString()
{
 return Racer.ToString + result;
}

But sadly, this does not work :( 但可悲的是,这不起作用:(

Any ideas? 有任何想法吗?

Thanks for help 感谢帮助

As ToString() is not static, you can't call Racer.ToString(). 由于ToString()不是静态的,因此无法调用Racer.ToString()。

You have to instanciate a Racer object an then call ToString() on it. 您必须实例化Racer对象,然后在其上调用ToString()。

Edit: this probably nearer to what you are intending: 编辑:这可能更接近您的意图:

class Racer
{
    public string name, surname;
    public Time Time { get; set; }

    public override string ToString()
    {
        return surname + name + "(" + Time.ToString() + ")";
    }           
}

class Time
{
    DateTime startTime, finishTime, result;

    public override string ToString()
    {
        TimeSpan elapsedTime = finishTime - startTime;
        return elapsedTime.ToString();
    }
} 

Each Racer was a Time property which represents how long they took to run the race. 每个“赛车手”都是一个“时间”属性,表示他们花了多长时间运行了比赛。 The Racer.ToString method calls the Time.ToString method to include the race time, along with the Racer's name. Racer.ToString方法调用Time.ToString方法以包括竞赛时间以及Racer的名称。

Generally, the easiest way to get self-maintainable ToString methods is to use a library for just exactly that. 通常,获取可自我维护的ToString方法的最简单方法是使用一个库来实现该目的。 eg. 例如。 from https://github.com/kbilsted/StatePrinter/blob/master/doc/AutomatingToStrings.md 来自https://github.com/kbilsted/StatePrinter/blob/master/doc/AutomatingToStrings.md

class AClassWithToString
{
  string B = "hello";
  int[] C = {5,4,3,2,1};

  // Nice stuff ahead!
  static readonly Stateprinter printer = new Stateprinter();

  public override string ToString()
  {
    return printer.PrintObject(this);
  }
}

notice how the ToString will automatically update itself when you introduce new fields into the class. 请注意,当您在类中引入新字段时, ToString将如何自动自我更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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