Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I converted vb6 code into C# but facing an error.
Suppose we have Person 
--------------------------------------------
 
public class Person
{
private int intID;
private string strName;
private string strSurname;
public Person()
{
intID = -1;
strName = "";
strSurname = "";
}
public int ID
{
get
{
return intID;
}
set
{
intID = value;
}
}
public string Name
{
get
{
return strName;
}
set
{
strName = value;
}
}
public string Surname
{
get
{
return strSurname;
}
set
{
strSurname = value;
}
}
}
 --------------------------------------------------------------------
I want to write this class into file. I was using 
Person entP = new Person();
entP.ID = 1;
entP.Name= "john";
entP.Surname ="doe";
FileSystem.FileOpen(1, "test_file", OpenMode.Binary);
FileSystem.FilePutObject(1, entP,-1);
FileSystem.FileClose(1);
But it throws an error
Unhandled Exception: System.ArgumentException: File I/O with type 'Person' is not valid.
It seems FilePutObject does not support classes. Is there alternative way to do same task?
Please guide
thanks 


What I have tried:

Person entP = new Person();
entP.ID = 1;
entP.Name= "john";
entP.Surname ="doe";
FileSystem.FileOpen(1, "test_file", OpenMode.Binary);
FileSystem.FilePutObject(1, entP,-1);
FileSystem.FileClose(1);
But it throws an error
Unhandled Exception: System.ArgumentException: File I/O with type 'Person' is not valid.
Posted
Updated 14-Jun-18 23:18pm
Comments
Richard MacCutchan 15-Jun-18 5:15am    
I suspect that this method can only handle known classes that contain meta data that tells the FilePutObject method what the object contains. There are better ways to serialize in C#, and Google will find you samples.

1 solution

I would not recommend using FilePutObject - the data files it generates will not be compatible with those produced by your existing VB6 code anyway, so you are better off migrating to a more modern data storage, such as XML or JSON anyway.
JSON is very easy, particularly if you use one of the open source packages for it: Json.NET - Newtonsoft[^] is the one I use.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900