Public class is not public
I'm trying to serialize some public class, but it doesn't compleate correctly, I receive an error message that class is not public. there is an all code of my console application (from MSDN):
Code:
Imports System.IO
Imports System.Xml.Serialization
Module Module1
Sub Main()
SerializeObject("person.xml")
End Sub
Private Sub SerializeObject(ByVal filename As String)
Dim serializer As New XmlSerializer(GetType(OrderedItem))
' Create an instance of the class to be serialized.
Dim i As New OrderedItem()
' Set the public property values.
With i
.ItemName = "Widget"
.Description = "Regular Widget"
.Quantity = 10
.UnitPrice = CDec(2.3)
End With
' Writing the document requires a TextWriter.
Dim writer As New StreamWriter(filename)
' Serialize the object, and close the TextWriter.
serializer.Serialize(writer, i)
writer.Close()
End Sub
' This is the class that will be serialized.
Public Class OrderedItem
Public ItemName As String
Public Description As String
Public UnitPrice As Decimal
Public Quantity As Integer
End Class
End Module
how can i resolve this issue?
Re: Public class is not public
1) The class cannot be inside a module (I believe)
2) The class hasn't been marked with the Serializable attribute.
3) I'm not 100% sure about this, but I don't know if fields can be serialized, I think (and it's a big think) you have to implement them as actual properties.
-tg
Re: Public class is not public
I can't think of any valid reason to ever declare a class inside a module.
Re: Public class is not public
1. thank you, you really helped me
2. if you use XML serialization you should not mark class with Serializable attribut.
3. fields can be serialized but it is optionally
Re: Public class is not public
Quote:
Originally Posted by jmcilhinney
I can't think of any valid reason to ever declare a class inside a module.
i didn't know that before