garry27 Posted June 11, 2009 Share Posted June 11, 2009 Hi, I'm trying to serialize/deserialize data in vb. I've made a .dat file from a blank plain text file (not sure if this is suitable) which seems to connect ok, as do both the classes. Here's the code: Imports Microsoft.VisualBasic.ControlChars Imports System.IO Imports System.Runtime.Serialization.Formatters.Binary Public Class NewSalesPersonForm Inherits System.Windows.Forms.Form 'dec object array Private mySalesPerson() As SalesPersonClass.SalesPersonClass 'namespaces required 'dec primitive arrays 'dec private vars Dim fullName As String Dim startDate As Date Dim nextIndex As Integer Private Sub NewSalesPersonForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'declare file stream and binary formatter Dim fs As New FileStream("sales-people.dat", FileMode.Open) Dim bf As New BinaryFormatter 'read into array mySalesPerson = CType(bf.Deserialize(fs), SalesPersonClass.SalesPersonClass()) 'close file stream fs.Close() nextIndex = mySalesPerson.GetUpperBound(0) End Sub Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click 'Close() End Sub Private Sub btnAddSalesPerson_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddSalesPerson.Click 'error checking mySalesPerson.pFullName = txtFullName.Text 'errors here mySalesPerson.pStartDate = cbDate.Text 'errors here 'open filestream object to serial file Dim fs As New FileStream("sales-people.dat", FileMode.Create) 'declare binary formatter object Dim bf As New BinaryFormatter 'upload data from array into serial file bf.Serialize(fs, mySalesPerson) 'close filestream fs.Close() End Sub End Class <Serializable()> Public Class SalesPersonClass 'attributes Private fullName As String Private startDate As Date 'empty constructor Public Sub New() fullName = "" startDate = "" End Sub 'full constructor Public Sub New(ByVal vFullName As String, ByVal vStartDate As Date) fullName = vFullName startDate = vStartDate End Sub 'properties Public Property pFullName() As String Get Return fullName End Get Set(ByVal Value As String) fullName = Value End Set End Property Public Property pStartDate() As String Get Return startDate End Get Set(ByVal Value As String) startDate = Value End Set End Property End Class The problem i think is that I'm trying to instantiate an empty object. It may also be because I'm not specifying an index in mySalesPerson.pFullName but when i do attach an index (0 to indicate start of array) I get an error that says "object reference not set to an instance of an object". To be honest, I don't really know what's going on so any explanation on how to access objects contained in a dat file would be helpful as well. Another thing is, I've seen three different ways to instantiate objects and not had much luck with any of them. They are: Private mySalesPerson As SalesPersonClass.SalesPersonClass Private mySalesPerson As SalesPersonClass.SalesPersonClass() Private mySalesPerson() As SalesPersonClass.SalesPersonClass Private mySalesPerson() As SalesPersonClass.SalesPersonClass() all of these are perfectly acceptable, gramatically, in vb and I've seen at least 3 of them used in various different examples. I assume the last 2 examples declare that you want to make an array of objects?!?!?!? Quote Link to comment https://forums.phpfreaks.com/topic/161761-visual-basic-serialization/ Share on other sites More sharing options...
garry27 Posted June 11, 2009 Author Share Posted June 11, 2009 Any ideas people? Quote Link to comment https://forums.phpfreaks.com/topic/161761-visual-basic-serialization/#findComment-853737 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.