Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

First I will state the original Programming Challenge, then I will state my code

ID: 3633678 • Letter: F

Question

First I will state the original Programming Challenge, then I will state my code, then the warnings, which is where I am stuck.


Challenge:
Create an application that stores data about your DVD collection in a file. The application should have a structure to hold the following fields: Video Name, Year Produced, Running Time, and Rating. The application should allow the user to save the data to a file, search the file for a video by name, and print a report listing all the video records in the file.

Code:
Imports System.IO
Imports System.IO.FileStream
Structure Video
Dim videoName As String
Dim yearProduced As Integer
Dim runningTime As String
Dim rating As String
End Structure


Public Class Form1
Dim txtFile As StreamWriter
Dim searchFile As StreamReader

Private Sub menuItemSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menuItemSave.Click
Dim vRecord As Video
'Assigning values
vRecord.videoName = txtVideoName.Text
vRecord.yearProduced = Convert.ToInt32(txtYearProduced.Text)
vRecord.runningTime = txtRunTime.Text
vRecord.rating = txtRating.Text
'Opening file
txtFile = File.CreateText("VideoFile.txt")
'Writing data to file
txtFile.WriteLine(vRecord.videoName)
txtFile.WriteLine(vRecord.yearProduced)
txtFile.WriteLine(vRecord.runningTime)
txtFile.WriteLine(vRecord.rating)
txtFile.Close() 'Close file
clearFields()

End Sub

Private Sub menuPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menuItemPrint.Click
Dim report As String
report = "Report of Video Collection" + vbNewLine
'Open file
searchFile = File.OpenText("VideoFile.txt")
Try
'Read file
While Not searchFile.EndOfStream
report += searchFile.ReadLine() + " "
report += searchFile.ReadLine() + " "
report += searchFile.ReadLine() + " "
report += searchFile.ReadLine() + " "
report += vbNewLine

End While
Catch ex As Exception
End Try
'Show msg report
MessageBox.Show(report)

End Sub

Private Sub menuFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menuItemFind.Click
'Open file to search
searchFile = File.OpenText("VideoFile.txt")
Dim svname As String
Dim flag As Integer
flag = 0
'Get name from user
svname = InputBox("Enter Video Name")
Dim vSearchRecord As Video
Try
'Search
While Not searchFile.EndOfStream
vSearchRecord.videoName = searchFile.ReadLine()
vSearchRecord.yearProduced = searchFile.ReadLine()
vSearchRecord.runningTime = searchFile.ReadLine()
vSearchRecord.rating = searchFile.ReadLine()
'Compare
If vSearchRecord.videoName.Equals(svname) Then
flag = 1
Exit While
End If
End While
'Show results
If flag.Equals(1) Then
txtVideoName.Text = vSearchRecord.videoName.ToString()
txtYearProduced.Text = vSearchRecord.yearProduced.ToString()
txtRunTime.Text = vSearchRecord.runningTime.ToString()
txtRating.Text = vSearchRecord.rating.ToString()
Else
MessageBox.Show("Record Does Not Exist")
clearFields()
End If

Catch ex As Exception

End Try
End Sub

Private Sub menuItemClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menuItemClose.Click
Me.Close()
End Sub
Public Sub clearFields()
txtVideoName.Text = ""
txtYearProduced.Text = ""
txtRunTime.Text = ""
txtRating.Text = ""
End Sub

Private Sub menuItemAbout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menuItemAbout.Click
MessageBox.Show("Please close and try again.")
End Sub
End Class

Warnings:
Variable 'videoName' has been used before it has been assigned a value. A null reference exception could result at runtime.
Variable 'runningTime' has been used before it has been assigned a value. A null reference exception could result at runtime.
Variable 'Rating' has been used before it has been assigned a value. A null reference exception could result at runtime.

Issue:
When I perform a search for a video, a message does not show up letting me know that it was or was not found.

Thanks in advance.

Explanation / Answer

Hi,

The warnings aren't a problem -- they won't stop your code from working (like an error will). In this case, the warnings are saying that there may be certain times when those variables don't get set in your While loop, which is true if you don't find a matching video in your file.

If you want to get rid of the warnings, you can initialize each of the string values to an empty string before your loop, like this:

Dim vSearchRecord As Video
vSearchRecord.videoName = ""
vSearchRecord.runningTime = ""
vSearchRecord.rating = ""
Try
'Search
While Not searchFile.EndOfStream
...

But, with that said, you're code should work fine even if the warnings are there.

A bigger problem has to do with the fact that the way the program is written, only one video will ever be written out to the file. That's because of this line:

txtFile = File.CreateText("VideoFile.txt")

That'll create a new text file every time you save a video (so you'll lose whatever was already written there). You should use this instead:

txtFile = File.AppendText("VideoFile.txt")

That will create a file if one doesn't exist or will add on to the end of it if the file is already there.

Even without any changes, though, I was able to use your Find logic and have it behave as expected. When I searched using an existing video name (note the name must match exactly, including case) it displayed the matching video data in the text fields. When I searched for a non-existent video, it displayed the "Record Does Not Exist" message.

For your issue, does the program prompt you for a video name when you click Find? What happens next? Do you know how to set a breakpoint in the debugger (inside mnu_FindClick(...)) and step through your code?

Reply with more info / questions if you like.

Thanks,

Joe

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote