Serialize an instance

T

Tony Johansson

Hello!

If I for example want to Serialize an object of this class Product se below.
Will this cause any problems because of all the members is private ?
In all the examples I have seen is all the members field public and that
is not what they normally are.

public class Product
{
private long Id;
private string Name;
private Price;

public Product(long id, string name, double price, string notes)
{
Id = id;
Name = name;
Price = price;
Notes = notes;
}

public override string ToString()
{
return string.Format("{0}: {1} (${2:F2}) {3}", Id, Nmae, Price,
Notes);
}
}

//Tony
 
A

Alberto Poblacion

Tony Johansson said:
If I for example want to Serialize an object of this class Product se
below.
Will this cause any problems because of all the members is private ?
In all the examples I have seen is all the members field public and that
is not what they normally are.

It depends on how you serilize the class. If you use the XmlSerializer,
the private fields will not be serialized, so it won't work for your class.
But if you serialize with a BinaryFormatter or a SoapFormatter, the private
fileds ARE serialized.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top