473,480 Members | 3,062 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Does Serialization Work On Structures?

Does serialization work on custom data types via structures?

Dim bFormatter As New BinaryFormatter()
Dim sFile As FileStream = File.Create(FilePath)
Dim structure As myStruct
Dim val1 as Integer
Dim Val2 as String
End Structure
Dim aStruct as myStruct

aStruct.val1 = 123
aStruct.val2 = "some text"

bFormatter.Serialize(sFile, aStruct)
sFile.Close()

Everytime I use serialization on structures I get a PublicKeyToken=null is
not marked as serializable error. I can find much on serialization. Does
the object need some special interface to be serializ-able?
Nov 20 '05 #1
2 10283
Yes it does work. Make sure you marked your structure with the
<Serializable()> attribute. This tells the framework your object/structure
is ok to serialize:
<Serializable()> _
Structure foo
Dim val1 As Integer
Dim Val2 As String
End Structure

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim bFormatter As New
Runtime.Serialization.Formatters.Binary.BinaryForm atter
Dim sFile As System.IO.FileStream = System.IO.File.Create("c:\test")
Dim x As foo
x.val1 = 123
x.Val2 = "some text"
bFormatter.Serialize(sFile, x)
sFile.Close()
End Sub

--
Justin Weinberg

Designing a PrintDocument or creating .NET graphics?
Save time with GDI+ Architect.
For more information, visit http://www.mrgsoft.com
"Daryll SHatz" <ds****@cox.net> wrote in message
news:Og**************@TK2MSFTNGP12.phx.gbl...
Does serialization work on custom data types via structures?

Dim bFormatter As New BinaryFormatter()
Dim sFile As FileStream = File.Create(FilePath)
Dim structure As myStruct
Dim val1 as Integer
Dim Val2 as String
End Structure
Dim aStruct as myStruct

aStruct.val1 = 123
aStruct.val2 = "some text"

bFormatter.Serialize(sFile, aStruct)
sFile.Close()

Everytime I use serialization on structures I get a PublicKeyToken=null is
not marked as serializable error. I can find much on serialization. Does
the object need some special interface to be serializ-able?

Nov 20 '05 #2
Daryll,
As Justin stated, you need to mark the Structure as Serializable as he
demonstrated.

Also the following articles covers every thing you wanted to know about
serialization but were afraid to ask:

http://msdn.microsoft.com/msdnmag/issues/02/04/net/
http://msdn.microsoft.com/msdnmag/issues/02/07/net/
http://msdn.microsoft.com/msdnmag/issues/02/09/net/

Hope this helps
Jay

"Daryll SHatz" <ds****@cox.net> wrote in message
news:Og**************@TK2MSFTNGP12.phx.gbl...
Does serialization work on custom data types via structures?

Dim bFormatter As New BinaryFormatter()
Dim sFile As FileStream = File.Create(FilePath)
Dim structure As myStruct
Dim val1 as Integer
Dim Val2 as String
End Structure
Dim aStruct as myStruct

aStruct.val1 = 123
aStruct.val2 = "some text"

bFormatter.Serialize(sFile, aStruct)
sFile.Close()

Everytime I use serialization on structures I get a PublicKeyToken=null is
not marked as serializable error. I can find much on serialization. Does
the object need some special interface to be serializ-able?

Nov 20 '05 #3

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

Similar topics

0
812
by: Dave | last post by:
I was curious to know if anyone has suggestions about maintaining various versions of class definitions when using serialization to save data. Is there a "standard" way of handling backward...
2
3122
by: Todd | last post by:
I've seen others with this same dilema but no solutions. Is there a way to serialize an object containing private fields? What about private fields containing complex data structures such as custom...
0
1712
by: Todd | last post by:
What attributes/code do I need to add to a class to get GetObjectData to fire? I need to use XML serialization to serialize an object with private members including complex data structures which...
0
2940
by: uAsking | last post by:
I'm building a client side application that uses webservices. In the client application I'm inheriting from basic types used by the webservices, so the wevservice provides the classes and sets all...
2
1429
by: jacob navia | last post by:
I am starting to write a serialization module for structures using XML. This will be along the lines of: http://www.xml.com/lpt/a/1999/09/serialization/index.html Questions: 1) Has...
2
5157
by: Phil Da Lick! | last post by:
Hello, Anyone got any good examples of how to serialize a nested class? My outer class has got ArrayList members for inner classes, each with their own data. I need to serialise the lot into...
10
1380
by: Jesper | last post by:
Hi, I'm writing a program which is, and will continue to be, constantly extended with functionality that require information. Until now the source of this information has been found and modified...
0
3018
by: ronnotel | last post by:
I have integrated APIs from a third party into my framework. The third party is essentially a .Net wrapper on top of an existing set of C++ classes and structs. I want to pass-around objects in...
10
4000
by: Uma - Chellasoft | last post by:
Hai, I am new to VB.Net programming, directly doing socket programming. In C, I will be able to map the message arrived in a socket directly to a structure. Is this possible in VB.Net. Can...
0
7055
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
6920
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...
1
6760
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
7022
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...
0
5365
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4501
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3013
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3004
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
206
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.