Writing a class or struct to stream

J

John

How come something like this is not legal?

public class myRec
{
public string Name = "";
public string Address = "";
public bool Update = false;
}

BinaryWriter outStream = new BinaryWriter(new FileStream("file.dat",
FileMode.Create));
myRec mRec = new myRec();
mRec.Name = data.name;
mRec.Address = data.address;
mRec.Update = data.update;
outStream.Write(mRec);

I get the following errors

Error 1 The best overloaded method match for
'System.IO.BinaryWriter.Write(bool)' has some invalid arguments
C:\code\Form1.cs 90 13 File1

Error 2 Argument '1': cannot convert from 'File1.Form1.NodeRec' to 'bool'
C:\code\Form1.cs 90 27 File1

I am assuming that I have to add one value at a time to the writer (which
works),
then write it out to the stream instead of just trying to write the class to
the stream.


John
 
J

Jon Skeet [C# MVP]

John said:
How come something like this is not legal?

public class myRec
{
public string Name = "";
public string Address = "";
public bool Update = false;
}

BinaryWriter outStream = new BinaryWriter(new FileStream("file.dat",
FileMode.Create));
myRec mRec = new myRec();
mRec.Name = data.name;
mRec.Address = data.address;
mRec.Update = data.update;
outStream.Write(mRec);

I get the following errors

Error 1 The best overloaded method match for
'System.IO.BinaryWriter.Write(bool)' has some invalid arguments
C:\code\Form1.cs 90 13 File1

Indeed. Which overload did you expect to use?
Error 2 Argument '1': cannot convert from 'File1.Form1.NodeRec' to 'bool'
C:\code\Form1.cs 90 27 File1

I am assuming that I have to add one value at a time to the writer (which
works), then write it out to the stream instead of just trying to write
the class to the stream.

Well, an alternative is to use one of the serializers -
BinarySerializer or XmlSerializer, for instance.
 
J

John Rogers

I have never used serialization before so I did not know about it. Even
after
reading about it I still can't get it to work.

In some of the other languages you have like an ItemData property. In c#
I am trying to attain the same kind of functionality, but so far I am not
getting
anywhere with it. I read a bit on serializing data, but I couldn't find any
good
examples to really show me how to use it in mycase.

String DATFILE = szPath + "\\Test.dat";

fStream = new FileStream(FILE_NAME, FileMode.Create);
binWriter = new BinaryWriter(fStream);

LoopingFunctionToGetMyData();

binWriter.Close();


public class myRec
{
public string Name = "";
public string Address = "";
public bool Update = false;
}

myRec mRec = new myRec();
mRec.Name = data.name;
mRec.Address = data.address;
mRec.Update = data.update;
// outStream.Write(mRec);

BinaryFormatter binFormatter = new BinaryFormatter();
binFormatter.Serialize(fStream, myRec);

I can't get past this, because I am looping over 100 times and filling my
class with the info
I need. Apparently my serialize os not working.

Can you show me an example or fill in the blanks of what I am missing.


Thanks


John
 
C

Christopher Van Kirk

How come something like this is not legal?

public class myRec
{
public string Name = "";
public string Address = "";
public bool Update = false;
}

BinaryWriter outStream = new BinaryWriter(new FileStream("file.dat",
FileMode.Create));
myRec mRec = new myRec();
mRec.Name = data.name;
mRec.Address = data.address;
mRec.Update = data.update;
outStream.Write(mRec);

I get the following errors

Error 1 The best overloaded method match for
'System.IO.BinaryWriter.Write(bool)' has some invalid arguments
C:\code\Form1.cs 90 13 File1

Error 2 Argument '1': cannot convert from 'File1.Form1.NodeRec' to 'bool'
C:\code\Form1.cs 90 27 File1

I am assuming that I have to add one value at a time to the writer (which
works),
then write it out to the stream instead of just trying to write the class to
the stream.


John

The BinaryFormatter class is what you're looking for.
 
D

Duy Lam

John said:
How come something like this is not legal?

public class myRec
{
public string Name = "";
public string Address = "";
public bool Update = false;
}

BinaryWriter outStream = new BinaryWriter(new FileStream("file.dat",
FileMode.Create));
myRec mRec = new myRec();
mRec.Name = data.name;
mRec.Address = data.address;
mRec.Update = data.update;
outStream.Write(mRec);

I get the following errors

Error 1 The best overloaded method match for
'System.IO.BinaryWriter.Write(bool)' has some invalid arguments
C:\code\Form1.cs 90 13 File1

Error 2 Argument '1': cannot convert from 'File1.Form1.NodeRec' to 'bool'
C:\code\Form1.cs 90 27 File1

I am assuming that I have to add one value at a time to the writer (which
works),
then write it out to the stream instead of just trying to write the class to
the stream.


John

This is a code snip to save and retrieve data of class. Using erialization:


CustomerInfo customer = new CustomerInfo();
customer.Age = 20;
customer.Name = "Kate Moss";
using (System.IO.Stream outStream = IO.File.Create(@"c:\file.data"))
{
System.Runtime.Serialization.IFormatter formater = new
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
formater.Serialize(outStream, customer);
}

using (System.IO.Stream outStream =
System.IO.File.OpenRead(@"c:\file.data"))
{
System.Runtime.Serialization.IFormatter formater = new
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
customer = formater.Deserialize(outStream) as CustomerInfo;
}

class CustomerInfo
{
string _name;
int age;

public string Name
{
get { return _name; }
set { _name = value; }
}


public int Age
{
get { return age; }
set { age = value; }
}
}

Hope this help.
 

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