Dark Star1
Dark Star1

Reputation: 7413

How do I return a compound string in an overridden ToString function?

I'm not sure how to make the question clearer but this is bascially the problem:

I have a class that is dervived from another one of my classes. The base class has an overridden Tostring function (returns 2 strings separated by a colon).

The problem is that my derived class can have an array of strings or just the one string so when I override the ToString function I need to return the base class as well as the array of strings (separated by "\n") in the derived class. Now I'm wondering what would be the best way to do this; should I return an array of strings( if possible) or do I have no choice but to use the Stringbuilder class? If there's another way to do this please tell.. All ideas welcomed no matter how crazy they may be :)

Upvotes: 0

Views: 1013

Answers (6)

Ubiquitous Che
Ubiquitous Che

Reputation: 349

As a general rule, you're better off using Stringbuilder than concatenating strings using a long line of + operations.

If you do the following:

string sTemp = "This i" + "s an exa" + "ple string tha" + "t was broken.";

This will actually create seven strings in memory.

  1. "This i"
  2. "s an exa"
  3. "This is an exa"
  4. "ple to"
  5. "This is an example string tha"
  6. "t was broken."
  7. "This is an example string that was broken."

The use of the StringBuilder for the same functionality will only create five, and as the scale goes up the StringBuilder will do a faster job of concatenating all these strings together than simply running a bunch of + operators one after the other.

This is particularly true when your list of concatenations is not predetermined ahead of time, as is the case with your array of string objects.

Upvotes: 1

Kevin LaBranche
Kevin LaBranche

Reputation: 21088

As Henri mentioned, return the base.ToString() then you can replace the colon with the \n using String.Replace and then combine it with your newly derived string.

Of course, you could just use a string in your derived class that takes the form of "string \n string" and in the ToString function you simply just perform step the replace and then combine the two strings together. No array, no stringbuilder, needed. If you think their will be a low # of string values to put together this may be the simplest option. No arrays or stringbuilder.

Upvotes: 1

Richard Lennox
Richard Lennox

Reputation: 143

What do you mean by "return the base class as well as the array of strings"?

If you meant the ToString() method of the base class you could try:

base.ToString() + "\n" + String.Join("\n", theStringArray );

Upvotes: 2

DevDevDev
DevDevDev

Reputation: 5177

It seems to me that there is something flawed in your design, but given what you said, there is no way to return an array of strings from the ToString() method. So you will have to delimit the base class's ToString() return value with the derived.

Such as

public override string ToString()
{
   return base.ToString() + ':' + "whatever";
}

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1502316

When overriding ToString you have to return just a string. You can't return any other type. It sounds like you might want something like this:

public override ToString()
{
    return base.ToString() + "\n" + string.Join("\n", stringArray);
}

That's assuming you want to return a string such as:

base1:base2
this1
this2
this3

where the line separators are \n.

Upvotes: 12

Henri
Henri

Reputation: 5113

You can use the return value of the ToString() from the base-class.

You can access it by

base.ToString();

Upvotes: 0

Related Questions