I get the distinct feeling that this should be a set of extension methods instead of a constructor or regular method. It has that code smell
about it.
MSDN Reference of Extension Methods
Also, you should use more proper naming conventions. (Name
instead of name
, etc.) And you don't need those Try/Catch/Finally
blocks. Lastly, you seem to be missing the <Serializable>
attribute.
Take your initial code
class data
public name as string
public cmd as Commands
public message as String
Public Sub New(ByVal byteData As Byte())
Try
' convert byte array to memory stream
Using _ms As New MemoryStream(byteData)
' create new BinaryFormatter
Dim _bf As New BinaryFormatter()
' set memory stream position to starting point
_ms.Position = 0
' Deserializes a stream into an object graph and return as a object.
dim oData as data =_bf.Deserialize(_ms)
name = oData.name
message = oData.Message
cmd = oData.cmd
End Using
Catch ex As Exception
Throw
End Try
End Sub
'Converts the Data structure into an array of bytes
Public Function ToByte() As Byte()
Dim bf As New BinaryFormatter()
Try
Using ms As New MemoryStream()
bf.Serialize(ms, Me)
Return ms.ToArray()
End Using
Catch ex As SerializationException
Return Nothing
Finally
bf = Nothing
End Try
End Function
end class
And let's rewrite it to be more effective.
<Serializable>
Class Data
Public Name As String
Public Cmd As Commands
Public Message As String
End Class
<Extension>
Public Sub FromByteArray(ByVal data As Data, ByVal byteArray As Byte())
Using _ms As New MemoryStream(byteArray)
' create new BinaryFormatter
Dim _bf As New BinaryFormatter()
' set memory stream position to starting point
_ms.Position = 0
' Deserializes a stream into an object graph and return as a object.
Dim oData As Data = _bf.Deserialize(_ms)
data.Name = oData.Name
data.Message = oData.Message
data.Cmd = oData.Cmd
End Using
End Sub
<Extension>
Public Function ToByteArray(ByVal data As Data)
Dim bf As New BinaryFormatter()
Using ms As New MemoryStream()
bf.Serialize(ms, data)
Return ms.ToArray()
End Using
End Function
Do note: Extension
methods can only be declared at the module level in VB.NET.
This technique very gracefully separates the responsibility of the class and it's serialization. The class should be responsible for handling it's work, whereas the Serialization/Deserialization can be done in an extension method, which makes it appear to be part of the class, but it doesn't need to be.
This, and the modifications by Heslacher, should get you much more desirable code. (I did not include any modifications he proposed, except the obvious code cleaning.)
Sample usage:
Sub Main()
Dim data As New Data()
data.Name = "Test"
Dim ByteArray As Byte() = data.ToByteArray()
data.FromByteArray(ByteArray)
Console.WriteLine(data.Name)
Console.ReadLine()
End Sub
That will print Test
, not only proving that this method works, but also proving, I think, your original design was flawed. How do you create an empty Data
object if the only constructor for it requires a Byte-Array?
New
line), please fix it; (B) Why are you restricting it to a byte array? Your object should be able to deserialize itself from any kind of stream. At least write another overload forNew
, and another overload for the serializer that writes to any stream. \$\endgroup\$ – Snowbody May 4 '15 at 3:07Commands
? Struct or class ? \$\endgroup\$ – Heslacher May 5 '15 at 4:53