Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

Writing a class or struct to stream

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
John - 07 Mar 2008 18:01 GMT
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
Jon Skeet [C# MVP] - 07 Mar 2008 18:56 GMT
> How come something like this is not legal?
>
[quoted text clipped - 18 lines]
> '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.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

John Rogers - 07 Mar 2008 21:09 GMT
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

>> How come something like this is not legal?
>>
[quoted text clipped - 30 lines]
> Well, an alternative is to use one of the serializers -
> BinarySerializer or XmlSerializer, for instance.
Jon Skeet [C# MVP] - 08 Mar 2008 19:25 GMT
> 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.

Well, what's happening when you try?

Could you post a short but *complete* program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Christopher Van Kirk - 08 Mar 2008 01:36 GMT
>How come something like this is not legal?
>
[quoted text clipped - 28 lines]
>
>John

The BinaryFormatter class is what you're looking for.

Signature

Posted via a free Usenet account from http://www.teranews.com

Duy Lam - 09 Mar 2008 14:30 GMT
> How come something like this is not legal?
>
[quoted text clipped - 28 lines]
>
> 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.

Signature

Thanks,
Duy Lam Phuong

John Rogers - 09 Mar 2008 15:39 GMT
Appreciate it Duy.

> This is a code snip to save and retrieve data of class. Using
> erialization:
[quoted text clipped - 3 lines]
> customer.Name = "Kate Moss";
> using (System.IO.Stream outStream = IO.File.Create(@"c:\file.data"))

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.