MissDizy
MissDizy

Reputation: 115

Structures of Inderminate Length

I am trying to understand Structures. Here is what I have

Structure Tbook
    Dim ISBN As String
    Dim Title As String
    Dim Price As Double
    Dim YearOfPub As Integer
End Structure
Dim books(2) As Tbook

Seems simple enough. I then try to populate it. However, I used a While Loop because I don't necessary want to fill it with 3 records. Instead I want to choose when to terminate data entry. Lets pretend I add 1 record not 3

Sub myStructure()
    Dim answer As Char
    Dim i As Integer = 0
    While i < 2
        Console.WriteLine("Enter details")
        Console.WriteLine("ISBN : ")
        books(i).ISBN = Console.ReadLine()
        Console.WriteLine("Title : ")
        books(i).Title = Console.ReadLine()
        Console.WriteLine("Price : ")
        books(i).Price = Console.ReadLine()
        Console.WriteLine("Year of Publication : ")
        books(i).YearOfPub = Console.ReadLine()
        i = i + 1
        If i < 2 Then
            Console.WriteLine("Add another book? Y/N")
            answer = Console.ReadLine().ToUpper
            If answer = "N" Then
                i = 3
            End If
        End If
    End While

Finally, I want to print out what I have. I have used a For Loop here which is obviously wrong. This code will print out 3 records but in my example - only 1 of these will have data.

For i = 0 To 2
        Console.WriteLine("==================================")
        Console.WriteLine("ISBN : " & books(i).ISBN)
        Console.WriteLine("Title : " & books(i).Title)
        Console.WriteLine("Price : " & books(i).Price)
        Console.WriteLine("Year of Publication : " & books(i).YearOfPub)
        Console.WriteLine("==================================")
        Console.WriteLine("")
Next
Console.ReadKey()

Piddling around with code like this leaves me with a few questions which may help others

a) Is it possible to create a structure of indeterminate size?

b) If not must you always 'fill' a structure with records

c) If you can 'partially fill' one, how do you print just these records

Many thanks to anyone with patience to explain this to me (and hopefully others)

Upvotes: 1

Views: 59

Answers (1)

Steve
Steve

Reputation: 216263

This is your code rewritten to use a generic List(Of TBook)

' This should be declared at the global level if you want to use'
' it everywhere in this class or module.'
Dim books As List(Of Tbook) = new List(Of Tbook)

Sub myStructure()
    Dim answer As Char
    Dim i As Integer = 0
    While True 
        Dim book As Tbook
        Console.WriteLine("Enter details")
        Console.WriteLine("ISBN : ")
        book.ISBN = Console.ReadLine()
        Console.WriteLine("Title : ")
        book.Title = Console.ReadLine()
        Console.WriteLine("Price : ")
        Dim input  = Console.ReadLine()
        Double.TryParse(input, book.Price)
        Console.WriteLine("Year of Publication : ")
        book.YearOfPub = Console.ReadLine()
        books.Add(book)
        Console.WriteLine("Add another book? Y/N")
        answer = Console.ReadLine().ToUpper
        If answer = "N" Then
            Exit While
        End If
    End While
End Sub

Here instead of having an array of fixed size you have a variable of type List(Of TBook) and you could Add a TBook inside a loop until you stop the input loop.

Notice that being TBook a structure you don't need to allocate a new element at each loop but you could add it directly to the list. Finally take care on the input of the Price field. It is a double and assigning it directly from the ReadLine that return a string is something to be avoided. You have Option Strict set to Off otherwise this code could not be compiled.

Of course reading from a List(Of TBook) is

For Each book In books
    Console.WriteLine("==================================")
    Console.WriteLine("ISBN : " & book.ISBN)
    Console.WriteLine("Title : " & book.Title)
    Console.WriteLine("Price : " & book.Price)
    Console.WriteLine("Year of Publication : " & book.YearOfPub)
    Console.WriteLine("==================================")
    Console.WriteLine("")
Next

Upvotes: 1

Related Questions