18

I am doing a test project to learn about XML serialization of an object, and I am getting an odd runtime error:

namespace SerializeTest
{

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }



    private void serializeConnection(Conn connection)
    {
        XmlSerializer serializer = new XmlSerializer(typeof(Conn));
        TextWriter textWriter = new StreamWriter(@"serialized.xml");
        serializer.Serialize(textWriter, connection);
        textWriter.Close();
    }

    static List<Conn> deserializeConnection()
    {
        XmlSerializer deserializer = new XmlSerializer(typeof(List<Conn>));
        TextReader textReader = new StreamReader(@"serialized.xml");
        List<Conn> connectionList;
        connectionList = (List<Conn>)deserializer.Deserialize(textReader);
        textReader.Close();

        return connectionList;
    }

    private void btnSerialize_Click(object sender, EventArgs e)
    {
        Conn conn = getConnection();
        serializeConnection(conn);

    }

    private Conn getConnection()
    {
        Conn connection = new Conn();
        connection.connectionName = txtName.Text;
        connection.address = txtAddress.Text;
        connection.height = 2542;
        connection.width = 4254;
        connection.password = txtPassword.Text;
        connection.smartSizing = false;
        connection.username = txtUsername.Text;
        connection.port = 474;
        return connection;
    }

    private void btnDeserialize_Click(object sender, EventArgs e)
    {
        int count = deserializeConnection().Count;
        lblStatus.Text = "Count: " + count;
    }
}

class Conn
{
    public Conn()
    {
    }
    public string connectionName { get; set; }
    public int height { get; set; }
    public int width { get; set; }
    public string address { get; set; }
    public string username { get; set; }
    public string password { get; set; }
    public int port { get; set; }
    public bool smartSizing { get; set; }
}

}

The Class is public - I don't understand what could be causing this error. Any help would be appreciated.

BrendanMcK
  • 14,252
  • 45
  • 54
Thad Blankenship
  • 2,242
  • 3
  • 16
  • 16

1 Answers1

45

The Class is public

No it's not. Here's the declaration:

class Conn
{
    ...
}

You haven't specified any access modifiers, so it's defaulting to internal (assuming it's non-nested). Just because it's got a public constructor doesn't make it public. You need to make it public explicitly:

public class Conn
{
    ...
}
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • I apologize, I thought class visibility was determined by constructor visibility. Thanks Jon. – Thad Blankenship Jun 30 '12 at 21:02
  • 8
    @ThadBlankenship: No need to apologize - it means you've learned something :) – Jon Skeet Jun 30 '12 at 21:07
  • But to make sure, Conn is not in the same namespace as the Form right? – MBen Jun 30 '12 at 21:42
  • 2
    @MBen: Why would that matter? Namespaces are completely orthogonal to accessibility - and in this case it's *serialization* which is complaining anyway. – Jon Skeet Jul 01 '12 at 06:19
  • Another determinant of class visibility is nestedness; a class that is nested is private. In certain circumstances, such a class must be explicitly marked as public, even for use within the outer class! – David A. Gray Oct 25 '18 at 16:39
  • @DavidA.Gray: You can always *refer* to a nested class within the containing class. You can't refer to it in the signature of a public method etc without making it public as well though. – Jon Skeet Oct 25 '18 at 17:55