0

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
3
  • Looks like vb6 or vb.net to me, definitely not vba - you might want to edit the tags and try a different audience :) Commented May 17, 2016 at 16:04
  • Yeah, that's VB.Net. You'll get more VB.Net fish with a VB.Net tag. :) checktechno.blogspot.com/2013/04/… Commented May 17, 2016 at 16:21
  • If you really use VB.NET, and not VB6 or something older, you should use the .NET classes and methods for creating the file. Also, please define "binary file", there are many types of binary files. Commented May 17, 2016 at 17:28

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.