9

A ToString Replacement that Recognizes Different Data Types

 2 years ago
source link: https://www.codeproject.com/Tips/5316291/A-ToString-Replacement-that-Recognizes-Different-D
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

A ToString Replacement that Recognizes Different Data Types

%7B3ab45c9d-ef7b-4a69-b332-0248219ff29c%7D.jpg

Tomasz Malicki

Rate me:
star-fill-lg.png
star-fill-lg.png
star-fill-lg.png
star-empty-lg.png
star-empty-lg.png
star-empty-lg.png
2.22/5 (2 votes)
28 Oct 2021CPOL1 min read
Most data types have a built-in ToString method, but not all, and what's more, the default method can throw an error if, for example, the object is null.
Not all data types have a built-in ToString method. Moreover, built-in methods can throw an error if, for example, an object is empty, or can only return the type name. This tip shows how to build a universal method (for example in extensions) that recognizes the type of data and converts it into a meaningful text string.

Using the Code

The method can be placed directly in the program class or extension class. Remember to remove the static and this declarations if you are using a dynamic class.

Copy Code
public static string AsString(this object oValue, bool ClearlyNamed = false)
{
    if (oValue == null) return ClearlyNamed ? "<null>":"";

    if (oValue.GetType() == typeof(string)) return oValue as string;

    if (oValue.GetType() == typeof(string[])) return string.Join(";",(string[])oValue));

//...
//Recognition and processing of other types of data
//...

    return ClearlyNamed?("<"+oValue.getType().Name+">"):"";
}

In the first line, the method detects if the object is empty and returns an empty string or description.
Then, in subsequent if blocks, it recognizes what type of data we are trying to present as a string and converts the data into text. The last line returns a text value if the data is not null but we cannot (or do not want to) determine its type.

The optional parameter ClearlyName forces the method not to process an null value or an unknown type into an empty string, but a description of what the input is.

It depends on the imagination of the coder whether it introduces the recognition of the next possible types of input data and the ways of processing it into text.

Comments and Explanations

Colleague Phil.o noticed well that the originally used method of merging the text from the array (using string1 + = string2) is not optimal and StringBuilder should be used. I corrected the source code as noted, but used a simpler method using string.Join.

History

  • 28th October, 2021: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

%7B3ab45c9d-ef7b-4a69-b332-0248219ff29c%7D.jpg

Tomasz Malicki

Retired
Poland Poland

This member doesn't quite have enough reputation to be able to display their biography and homepage.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK