Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / June 2007

Tip: Looking for answers? Try searching our database.

deriving from Byte[] in C#

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
EnerPeter - 08 Jun 2007 08:46 GMT
Hi all,
is it possible to derive from an array type - in my case from Byte[]. The
purpose is to override the ToString() of Byte[] method in such a way that it
results in a string representing the Byte[] as a 'normal' string - i know
that only legal ASCIIs are in the array - and not resulting in
"System.Byte[]".
Thanks in advance
Jani Järvinen [MVP] - 09 Jun 2007 04:50 GMT
Hello!

> Is it possible to derive from an array type - in my case from Byte[]. The
> purpose is to override the ToString() of Byte[] method in such a way that
> it
> results in a string representing the Byte[] as a 'normal' string.

Unfortunately, the short answer is no. By looking at the documentation for
Array, it might appear that you could derive your own types from
System.Array, but in fact all (at least all I have used) .NET
languages/compilers refuse to allow this. For example, the C# compiler fails
with the error message is "NN cannot derive from special class
System.Array".

However, even if you could do this, deriving would be more difficult than it
sounds at first, since the compiler actually does quite a lot of so-called
"compiler magic" for you when you use a seemingly simple construct like
this:

byte[] myBytes = { 12, 83, 62 };

Since System.Array is an abstract class, the compiler must first create a
derived type behind the scenes. Also, there's code needed to initialize the
array -- you can see all this when you use ILDASM for example to view the
generated MSIL (intermediate language) code.

Finally, if you would derive your own byte array class, it would not work
the same as regular arrays. As noted by the MSDN documentation of the Array
class:

"However, only the system and compilers can derive explicitly from the Array
class. Users should employ the array constructs provided by the language."

So the bottom line is that you cannot do it. With forthcoming C# 3.0
extension methods you could make things look more convenient, but in the end
your option is to write some code to convert the byte array to a string. See
here for more information extension methods in C# 3.0:

http://msdn2.microsoft.com/en-us/library/ms364047(vs.80).aspx#cs3spec_topic3

Finally, here's a simple example of a method that converts a byte array to a
proper string:

-----------------
byte[] myBytes = { 72, 101, 108, 108, 111, 33 };
string str = System.Text.Encoding.ASCII.GetString(myBytes);
MessageBox.Show(str);
-----------------

This would show: "Hello!". If you wanted to get the real bytes instead, then
you could use something like this:

-----------------
MessageBox.Show(BytesToString(myBytes));

private string BytesToString(byte[] bytes)
{
 StringBuilder buffer = new StringBuilder();
 buffer.Append("{ ");
 foreach (byte b in bytes)
 {
   if (buffer.Length > 2) buffer.Append(", ");
   buffer.Append(b);
 }
 buffer.Append(" }");
 return buffer.ToString();
}
-----------------

This would show: "{ 72, 101, 108, 108, 111, 33 }".

Hope this helps!

Signature

Regards,

Mr. Jani Järvinen
C# MVP
Helsinki, Finland
janij@removethis.dystopia.fi
http://www.saunalahti.fi/janij/


Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.