473,394 Members | 1,817 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

how to make a ToString method for a property

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[i];
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();

--
rgds, Wilfried
http://www.mestdagh.biz
Nov 16 '05 #1
5 2038
Wilfried,

Any reason that you do not use this one?

Encoding.GetString

http://msdn.microsoft.com/library/de...tringtopic.asp

Cor
"Wilfried Mestdagh" <Wi**************@discussions.microsoft.com>
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[i];
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();

--
rgds, Wilfried
http://www.mestdagh.biz

Nov 16 '05 #2
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.

--
rgds, Wilfried
http://www.mestdagh.biz
Nov 16 '05 #3
Wilfried Mestdagh <Wi**************@discussions.microsoft.com> wrote:
I have a class that holds data in array of byte. It has property to get the
data like this (FBuffer is a byte[]):
<snip>
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
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" ?
Nov 16 '05 #5
Wilfried Mestdagh <Wi**************@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Denis Perelyubskiy | last post by:
Hello, I need to make an array of elements accross forms. My javascript skills, as evident from this question, are rather rudimentary. I tried to make an associative array and index it with...
6
by: RonG | last post by:
This might seem like a stupid question but I'd really appreciate an answer TextBox.Text is property yet it has a ToString() method. In other words, i can type in textBox1.Text.ToString() and it...
8
by: John Cobb | last post by:
Excuse me if I use some terminology incorrectly as I am not well versed in Object Orientation. In short I want to create an Enumerated type similar to the following Enum Monitor as Byte Small...
1
by: Vilem Sova | last post by:
I want to use a user-defined object in the 'Add' method to add items to a combo box. The help documentation says that if you use an object in the add method then the object's 'ToString' method is...
27
by: Just Me | last post by:
I made a Usercontrol that must have AutoScroll set to true when it is used. So I set it to True in the Load event. However the property still shows in the properties window when the control is...
8
by: Phil Jollans | last post by:
Hi, I am having difficulty overriding the ToString() method of CultureInfo using Visual Studio 2005. Exactly the same code works fine with Visual Studio .NET 2003. What I am doing is adding...
19
by: zzw8206262001 | last post by:
Hi,I find a way to make javescript more like c++ or pyhon There is the sample code: function Father(self) //every contructor may have "self" argument { self=self?self:this; ...
71
by: active | last post by:
In the main program I check to see if a certain form has been disposed. Does it make sense in that form's FormClosed event to do: Me.Dispose to make sure it is disposed the next time I check. Or...
9
by: Hans-Jürgen Philippi | last post by:
Hi group, let's say I have a 'Person' class with properties like 'FirstName', 'LastName', 'Birthday' and so on. Now I overload the 'Person' ToString() method with an implementation...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.