Marco Costa Marco Costa - 6 months ago 17
Vb.net Question

Saving binary file

Hey guys i am relatively new to computer science and i was wondering why my code isnt working. My teacher told me to design a business card and save it into a binary file, but when i save i check the file there is nothing there. and when i load the file nothing is outputted so i was wondering if anyone could help out

Code:

Imports System.IO

Module Module1

Dim firstname As String
Dim secondname As String
Dim address As String
Dim postcode As String
Dim telephone As String
Dim email As String
Structure Details
Dim firstname As String
Dim secondname As String
Dim address As String
Dim postcode As String
Dim telephone As String
Dim email As String
End Structure
Sub Main()
Console.WriteLine("Do you want to (l)oad, or (c)hose?")
Dim decision As String = Console.ReadLine.ToUpper
If decision = "L" Then
load()
Main()
End If
If decision = "C" Then
chose()
Console.WriteLine("Saving")
save()
Main()
End If
Console.Read()
End Sub
Private Sub load()
Console.WriteLine("File name?")
Dim filename As String = Console.ReadLine + ".txt"
If (File.Exists(filename)) Then
Dim det As Details = New Details()
FileOpen(1, filename, OpenMode.Binary)
FileGet(1, det)
FileClose()
firstname = det.firstname
secondname = det.secondname
address = det.address
postcode = det.postcode
telephone = det.telephone
email = det.email
Console.WriteLine("Firstname =" & firstname)
Console.WriteLine("secondname =" & secondname)
Console.WriteLine("address =" & address)
Console.WriteLine("postcode =" & postcode)
Console.WriteLine("telephone =" & telephone)
Console.WriteLine("email =" & email)
End If
End Sub
Private Sub save()
Console.WriteLine("Enter a name for your file")
Dim filename As String = Console.ReadLine() + ".txt"
Dim det As Details = New Details()
det.firstname = firstname
det.secondname = secondname
det.address = address
det.postcode = postcode
det.telephone = telephone
det.email = email

FileOpen(1, filename, OpenMode.Binary)
FilePut(1, det)
FileClose()
End Sub
Private Sub chose()
Console.WriteLine()
Console.WriteLine("Enter your first name")
Dim firstname As String = Console.ReadLine
Console.WriteLine("Enter your second name")
Dim secondname As String = Console.ReadLine
Console.WriteLine("Enter your adress ")
Dim address As String = Console.ReadLine
Console.WriteLine("Enter your postcode")
Dim postcode As String = Console.ReadLine
Console.WriteLine("Enter your telephone")
Dim telephone As String = Console.ReadLine
Console.WriteLine("Enter your email")
Dim email As String = Console.ReadLine
End Sub
End Module

Answer

You've already declared the firstname, secondname, etc variables globally so don't dimension them again in the chose Sub or they'll be different local variables.

It should be:

Private Sub chose()
        Console.WriteLine()
        Console.WriteLine("Enter your first name")
        firstname = Console.ReadLine
        Console.WriteLine("Enter your second name")
        secondname = Console.ReadLine
        Console.WriteLine("Enter your adress ")
        address = Console.ReadLine
        Console.WriteLine("Enter your postcode")
        postcode = Console.ReadLine
        Console.WriteLine("Enter your telephone")
        telephone = Console.ReadLine
        Console.WriteLine("Enter your email")
        email = Console.ReadLine
    End Sub