Results 1 to 5 of 5

Thread: Public class is not public

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2008
    Posts
    24

    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?

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Public class is not public

    I can't think of any valid reason to ever declare a class inside a module.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jan 2008
    Posts
    24

    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

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jan 2008
    Posts
    24

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width