how to make a ToString method for a property

G

Guest

Hi,

I have a class that holds data in array of byte. It has property to get the
data like this (FBuffer is a byte[]):

public byte[] Data
{
get { return FBuffer; }
}

But for display / debugging purposes I have a property that returns the data
as a string like this:

public string DataToString
{
get
{
StringBuilder sb = new StringBuilder(FLength + 1);
int i = 0;
while (i < FLength) {
char chr = (char)FBuffer;
sb.Append(chr);
i++;
}
return sb.ToString();
}
}

It woks, but it would be nicer to have a .ToString() or something that I can
add to the property Data. Is this possible and if yes, how should I code this?

eg now:
byte[] b = Buf.Data;
string s = Buf.DataToString;

and I like to have something as:
string s = Buf.Data.ToString();
 
G

Guest

Hi Cor,
Any reason that you do not use this one?
Encoding.GetString

No reason, I just did not know the existance of it :) It works nice, however
I'm still curious how to implement a ToString method.
 
J

Jon Skeet [C# MVP]

Wilfried Mestdagh said:
I have a class that holds data in array of byte. It has property to get the
data like this (FBuffer is a byte[]):

It woks, but it would be nicer to have a .ToString() or something that I can
add to the property Data. Is this possible and if yes, how should I code this?

<snip>

You can't do it directly, because you can't add methods to byte[].
However, you could create a class which contained the byte[], overrode
ToString, and had a conversion (explicit or implicit) to byte[]. You
then return an instance of that class in your property.
 
G

Guest

Hi Jon,
You can't do it directly, because you can't add methods to byte[].
However, you could create a class which contained the byte[], overrode
ToString, and had a conversion (explicit or implicit) to byte[]. You
then return an instance of that class in your property.

this looks difficult for a beginner in NET like me. However I dont
understeand all words because English is not my native language. for example
what do you mean by "explicit or implicit" ?
 
J

Jon Skeet [C# MVP]

Wilfried Mestdagh said:
You can't do it directly, because you can't add methods to byte[].
However, you could create a class which contained the byte[], overrode
ToString, and had a conversion (explicit or implicit) to byte[]. You
then return an instance of that class in your property.

this looks difficult for a beginner in NET like me.

User-defined type conversions aren't *very* difficult, but I'm not sure
I'd advise a beginner to use them, to be honest. I don't generally like
them much anyway - especially implicit ones.
However I dont understeand all words because English is not my native
language. for example what do you mean by "explicit or implicit" ?

Explicit conversions required you to put cast-like syntax. Implicit
conversions don't. So you could do:

byte[] b = (byte[]) foo.Data; // Explicit conversion
or
byte[] b = foo.Data; // Implicit conversion

Look up "User-Defined Conversions Tutorial" in MSDN for more
information.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top