5 Replies - 407 Views - Last Post: 19 March 2013 - 10:46 PM Rate Topic: -----

#1 Ronan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 19-March 13

Cant load Structurethat has an array from binaryfile

Posted 19 March 2013 - 07:32 PM

Hi , I have a problem that has me driven mental. I've just started vb 4 months ago and i have a program to be done in 2 days . I'm really frustrated and about to quit the course I'm going so crazy!The program is a library program for storing books and members and i need to load save and edit a structure that has an array from a binary file.

here's where I'm at, ive tried to serialize it and deserialize it but i only get one passed book info

Imports System.IO
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.IO.BinaryReaader



<Serializable()> _
Public Structure BOOK
    Public bookISBN As Long, BookYearPub As Integer, bookNumCopies As Integer
    Public bookTitle As String, bookAuthor As String
    Public bookHardOrPaper As Boolean, booksOnLoan As Integer
End Structure

Public Class webform1
    Inherits System.Web.UI.Page
    Public Shared bookIndex As Integer = 0
    Public Shared librarySize As Integer = 3
    Public Shared books(librarySize) As BOOK




   Dim libraryStream As FileStream = File.Open(Server.MapPath("/Structure Bin Files/BookStructure.bin"), FileMode.Open)
                Dim binaryLibraryWriter As New BinaryFormatter
                binaryLibraryWriter.Serialize(libraryStream, books)





Dim libraryStream As FileStream = File.Open(Server.MapPath("/Structure Bin Files/BookStructure.bin"), FileMode.Open)
 Dim binaryLibraryWriter As New BinaryFormatter
            webForm1.books = binaryLibraryWriter.Deserialize(libraryStream)


Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Cant load Structurethat has an array from binaryfile

#2 Takk  Icon User is offline

  • D.I.C Head

Reputation: 40
  • View blog
  • Posts: 111
  • Joined: 08-March 13

Re: Cant load Structurethat has an array from binaryfile

Posted 19 March 2013 - 08:21 PM

You're missing quite a bit of code it seems and hard for me to tell where it's going wrong exactly without testing it but check out the link below, it looks like a fairly straight forward process from the info on the msdn library...
Hope this helps.

http://msdn.microsof...#code-snippet-1


also, how are you writing and debugging this? your first step would be to fix things like

Imports System.IO.BinaryReaader



as reader only has 1 a.

This post has been edited by Takk: 19 March 2013 - 08:23 PM

Was This Post Helpful? 0
  • +
  • -

#3 Ronan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 19-March 13

Re: Cant load Structurethat has an array from binaryfile

Posted 19 March 2013 - 08:34 PM

im using micrsoft windows visual web developer 2010 to debug

heres all my code
on my first form

Imports System.IO
Imports System.Runtime.Serialization.Formatters.Binary

<Serializable()> _
Public Structure LIBRARY
    Public bookISBN As Long, BookYearPub As Integer, bookNumCopies As Integer
    Public bookTitle As String, bookAuthor As String
    Public bookHardOrPaper As Boolean
    Public st As BOOK
End Structure

Public Class _default
    Inherits System.Web.UI.Page


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim libraryStream As FileStream = File.Open(Server.MapPath("/Structure Bin Files/BookStructure.bin"), FileMode.Open)
        If libraryStream.Length > 0 Then
            Dim binaryLibraryWriter As New BinaryFormatter
            webForm1.books = binaryLibraryWriter.Deserialize(libraryStream)
            MsgBox(webForm1.books(0).bookISBN, MsgBoxStyle.Critical, libraryStream.ToString)
            ' read it in...
            libraryStream.Close()

            FileOpen(1, Server.MapPath("/Structure Bin Files/BookStructure.bin"), OpenMode.Binary, OpenAccess.Read)
            FileGet(1, webForm1.books(webForm1.bookIndex))
          
            libraryStream.Close()
        Else
            MsgBox(" Library is EMPTY", MsgBoxStyle.Critical, "The Library")
            libraryStream.Close()
        End If


second form
Imports System.IO
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.IO.BinaryWriter

<Serializable()> _
Public Structure BOOK
    Public bookISBN As Long, BookYearPub As Integer, bookNumCopies As Integer
    Public bookTitle As String, bookAuthor As String
    Public bookHardOrPaper As Boolean, booksOnLoan As Integer
End Structure

Public Class webform1
    Inherits System.Web.UI.Page
    Public Shared bookIndex As Integer = 0
    Public Shared librarySize As Integer = 3
    Public Shared books(librarySize) As BOOK

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        For i = Date.Now.Year - 100 To Date.Now.Year
            listBoxYearPub.Items.Add(i)
        Next
    End Sub

    Protected Sub btnAddBook_Click(sender As Object, e As EventArgs) Handles btnAddBook.Click
        Dim uniqueISBN As Boolean = True

        If (CInt(listBoxYearPub.SelectedItem.Text)) <= 1970 And (txtISBN.Text.Length) <> 9 Then
            MsgBox("Please enter a 9 digit ISBN number", MsgBoxStyle.Critical, "The Library")
            txtISBN.Text = ("")
            SetFocus(txtISBN)
        ElseIf (CInt(listBoxYearPub.SelectedItem.Text)) >= 1970 And (listBoxYearPub.SelectedItem.Text) <= 1973 And (txtISBN.Text.Length) <> 10 Then
            MsgBox("Please enter a 10 digit ISBN number", MsgBoxStyle.Critical, "The Library")
            txtISBN.Text = ("")
            SetFocus(txtISBN)
        ElseIf (CInt(listBoxYearPub.SelectedItem.Text)) >= 1974 And (txtISBN.Text.Length) <> 13 Then
            MsgBox("Please enter a 13 digit ISBN number", MsgBoxStyle.Critical, "The Library")
            txtISBN.Text = ("")
            SetFocus(txtISBN)
        ElseIf txtISBN.Text = "" Then
            SetFocus(txtISBN)
        ElseIf txtAuthor.Text = "" Then
            SetFocus(txtAuthor)
        ElseIf txtBookTitle.Text = "" Then
            SetFocus(txtBookTitle)
        ElseIf txtNumBookCopies.Text = "" Then
            SetFocus(txtNumBookCopies)
        ElseIf books.Length <= bookIndex Then
            MsgBox("Library is full", MsgBoxStyle.Critical, "The Library")
            Server.Transfer("default.aspx")
        Else
            For i = 0 To bookIndex
                If txtISBN.Text = books(i).bookISBN Then
                    uniqueISBN = False
                End If
            Next
            If uniqueISBN Then

                With books(bookIndex)
                    .bookISBN = (txtISBN.Text)
                    .bookAuthor = txtAuthor.Text
                    .bookTitle = txtBookTitle.Text
                    .bookNumCopies = (txtNumBookCopies.Text)
                    .BookYearPub = listBoxYearPub.SelectedItem.Text
                    .bookHardOrPaper = radBtnHorP.SelectedIndex
                    .booksOnLoan = 0
                End With

                ' MsgBox("BookIndex =" & bookIndex & " ISBN" & txtISBN.Text & (books(bookIndex).bookISBN) & (books(bookIndex).bookAuthor) & (books(bookIndex).bookTitle) & (books(bookIndex).bookNumCopies) & (books(bookIndex).BookYearPub) & (books(bookIndex).bookHardOrPaper))
                Dim libraryStream As FileStream = File.Open(Server.MapPath("/Structure Bin Files/BookStructure.bin"), FileMode.Open)
                Dim binaryLibraryWriter As New BinaryFormatter
                binaryLibraryWriter.Serialize(libraryStream, books)
                FileOpen(1, Server.MapPath("/Structure Bin Files/BookStructure.bin"), OpenMode.Binary, OpenAccess.Write)
                FilePut(1, books)
                FileClose(1)

                '  Dim librarystream As FileStream = New FileStream(Server.MapPath("/Structure Bin Files/BookStructure.bin"), FileMode.Append)
                ' Dim binaryLibraryWriter As BinaryWriter = New BinaryWriter(librarystream)
                '  With binaryLibraryWriter
                '  .Write(CULng((books(bookIndex).bookISBN) & bookIndex & vbCrLf))
                ' .Write((books(bookIndex).bookAuthor) & bookIndex & vbCrLf)
                '  .Write((books(bookIndex).bookTitle) & bookIndex & vbCrLf)
                '  .Write(CUInt((books(bookIndex).bookNumCopies) & bookIndex & vbCrLf))
                ' .Write(CUInt((books(bookIndex).BookYearPub) & bookIndex & vbCrLf))
                ' .Write((books(bookIndex).bookHardOrPaper) & bookIndex & vbCrLf)

                libraryStream.Close()
                bookIndex += 1

                Server.Transfer("default.aspx")
                ' End With
            Else
                MsgBox("ISBN In Use", MsgBoxStyle.Critical, "The Library")
            End If
        End If
        MsgBox(bookIndex)

Was This Post Helpful? 0
  • +
  • -

#4 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2251
  • View blog
  • Posts: 9,435
  • Joined: 29-May 08

Re: Cant load Structurethat has an array from binaryfile

Posted 19 March 2013 - 10:02 PM

Why are choosing to use structure, rather that a class?
It is rare that you actually need the type semantics of a structure.

Also it helps think about program and its data structures, without a GUI.
Design the data structure, build the GUI on top of them later.

You can create your own type class and encapsulate the semantics of the data.
Spoiler


Also you seem to be using a lot of the OLD style vb6 ways of doing thing. Why?
For example:- MSGBox, FileOpen, FileGet

You are creating a new project / application using vb.net, step up and use the .net approaches.
It'll be more beneficial to you. Like the generic collection and the using structure
Using fs As New StreamReader( )
  theLib.Deserialise( fs ) 
End Using


See further down for the .Deserialize extension method.

Why use a fixed size array structure? Use a one of the generic collection types, like
 
 Dim myBooks As New List(Of Book) 
 myBooks.Add( thisBook )


You can even hide the implementation details inside another (more specific class). The expose only the functionality you need.
<Serializable()>
Public Class BookCollection
  Dim _Books As New List(Of Book)
 
  Public Sub AddBook(thisBook As Book)
    _Books.Add(thisBook)
  End Sub
End Sub


Then do
Dim myBook As New BookCollection
myBook.AddBook( someBook )



Even the serialization and deserialization can extracted out into it own extension method (in a module)
Imports System.Runtime.CompilerServices

Public Module Exts
  <Extension>
  Public Function Serialise(Of T)(data As T, toStream As IO.Stream) As Boolean
    Dim s = New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
    Try
      s.Serialize(toStream, data)
    Catch
      Return False
    End Try
    Return True
  End Function

  <Extension>
  Public Function Deserialise(Of T)(ByRef data As T, toStream As IO.Stream) As Boolean
    Dim s = New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
    Dim obj As Object = s.Deserialize(toStream)
    Try
      data = DirectCast(obj, T)
      Return True
    Catch ex As Exception
      data = Nothing
      Return False
    End Try
  End Function
End Module



Dim theLib As Library
' loading
theLib.Deserialize( fromThisStream )
' saving
theLib.Serialize( toThisStream )


This post has been edited by AdamSpeight2008: 19 March 2013 - 10:42 PM
Reason for edit:: missing a closing code tag

Was This Post Helpful? 0
  • +
  • -

#5 Ronan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 19-March 13

Re: Cant load Structurethat has an array from binaryfile

Posted 19 March 2013 - 10:38 PM

My tutor gave examples of structures in preparation for our assignment, beginning to think i need to totally change my approach

I used old code just trying everything for the last 6 days straight.

I forgot to say thanks !
Was This Post Helpful? 0
  • +
  • -

#6 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2251
  • View blog
  • Posts: 9,435
  • Joined: 29-May 08

Re: Cant load Structurethat has an array from binaryfile

Posted 19 March 2013 - 10:46 PM

A big change from vb6 to vb.net, is that a String is a new reference Type in a not a value type.

Tutorial:- Saving Your Objects
Tutorial:- Why Separation of Concerns Matters

If you are having problem with school class project, approach your tutor.
Ask questions, get answers. That's what they get paid to do.

As a programmer and designer you should have justifiable explanations for the design approach and decisions.
I doing x, y, z for the a, b, c reasons.

This post has been edited by AdamSpeight2008: 19 March 2013 - 10:59 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1