tags:

views:

165

answers:

6

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 :)

A: 

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

You can access it by

base.ToString();

Henri
+11  A: 

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.

Jon Skeet
Thank you very much
Dark Star1
You should use `Environment.NewLine` for the separator unless there's a specific requirement to not do so, where a `ToString()` implementation would generally not be one of the exceptions. :)
280Z28
I was just going by the "specification" in the string. I'm not even sure I agree with your recommendation though - it's like using `Encoding.Default`. Without knowing where the string is going to be used, no-one can possibly know what the right line terminator is.
Jon Skeet
+1  A: 

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";
}
DevDevDev
+2  A: 

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 );
Richard Lennox
I meant return the base class' ToString method as well as the string array from my derived class
Dark Star1
+1  A: 

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.

klabranche
+1  A: 

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.

Ubiquitous Che