Alright, I'll go over the whole thing from the top:
================================================
STEP 1: Understanding TYPE, DIM, and what is a record variable
================================================
you have this TYPE block, right?
Code:
TYPE VideoType
id AS INTEGER
title AS STRING * 50 (or however long)
actor AS STRING * 50 (or whatever)
rating AS STRING * 50 (or whatever)
END TYPE
think of your TYPE as like a template. each
element becomes a part of whatever variable you DIM as this TYPE:
Code:
DIM VideoIRentedLastWeek AS VideoType
This line of code makes a
variable called VideoIRentedLastWeek. Because it is defined as being of type "VideoType", it will inherit the
elements of the template: id, title, actor, and rating. VideoIRentedLastWeek is known as the
record variable.
Now, you can manipulate the record variable ELEMENTS just like you could any regular variable. For example, VideoIRentedLastWeek.id is defined as an INTEGER (in the TYPE block). Therefore, I can perform actions on it just like a regular integer:
Code:
VideoIRentedLastWeek.id = 5
VideoIRentedLastWeek.id = VideoIRentedLastWeek.id + 1
PRINT VideoIRentedLastWeek
This will output 6.
So, once again, to make a record variable, you do two things:
1. make a TYPE block template.
2. DIM a variable as this TYPE.
Now, you can do this in your main program, outside of your SUBs. But the variable that you DIM will not be accessable within your SUBs unless you create your record variable with DIM SHARED instead of DIM:
Code:
TYPE VideoType
id AS INTEGER
actor AS STRING * 50
rating AS STRING * 50
title AS STRING * 50
END TYPE
DIM SHARED Vid AS VideoType
There. Now I've made a record variable called "Vid" that I can use in all my SUBs. I will
not need any other DIM statements in my program.
================================================
STEP 2: Understanding RANDOM files.
================================================
A RANDOM file is a file that you can read to and write from that is broken up into
slots. Each slot will fit exactly one video. Each slot has a number associated with it. The numbers will go from 1 to basically infinity (there is a cap to this, but we won't worry about that, now).
To open your file, the command is:
Code:
OPEN "vidfile.dat" FOR RANDOM AS #1 LEN = LEN(Vid)
This says to open up the file for read & write access and specifies that each
slot fits exactly one video. The #1 is just the "file handle" which you use to reference which file you are referring to in your GET and PUT commands. Since we're only working with one file for this project, you will only be using #1.
Think of your RANDOM file as your video store. Each video has a shelf location, labelled by the video id, right? So if you have The Matrix on the shelf, and the shelf is labelled "23", then 23 is The Matrix's id number, and the information on this video should be in slot 23 in your RANDOM file.
You perform two commands on your RANDOM files, GET and PUT. GET retrieves information from the file from a specified slot, and saves it into a record variable:
This command says GET from file #1 (the only file we have open) the information stored in slot #23, and save this information into our record variable (Vid).
This command does the opposite. It says: Take the information in our record variable (Vid) and stuff it into slot #23 in the open file.
================================================
STEP 3: Your SUBs
================================================
Each of your subs performs one action: add a video to your file, remove a video from your file, search your file for a video, Edit a video in your file, or list all the videos in your file. Let's look at each of these actions, one by one.
AddVideo: To add a video, we will need to get the video information from the user, store it into the record variable, OPEN the file, PUT the record variable into the file in the right slot (i.e. on the right shelf), then CLOSE the file.
DeleteVideo: To delete a video, we will need to set the record variable (Vid) to blank values, OPEN the file, PUT the (blank) record variable into the file in the right slot, then CLOSE the file. See, we PUT a blank record variable into the file because this will overwrite any data that might have been in the file in that slot.
SearchVideo: To search for a video, we need to know what video we are looking for. If the user provides the video id, it is easy to find, because you know which slot in the file will have the information they are looking for. So, you get the id from the user, then you OPEN the file, GET the record variable from that slot, display it on the screen, then CLOSE the file.
EditVideo: Again, you need to know which video you're editing. So ask the user for the id. the OPEN the file and GET the record variable from that slot. Then display the information to the user, and see what they want to change. Any changes they make update the record variable. For example, if they want to edit the rating of the video, have them INPUT a new Vid.rating. Then, once they've finished their changes, you PUT the update record variable back into the same slot, then CLOSE the file.
ListVideos: This one is kind of tricky. You need some kind of loop that will go through the file, slot by slot, and see whether a video exists in each slot. If it does, you print it to the screen. The logic goes something like this:
OPEN the file
set a "counter" variable to 1 to indicate that we want to start at the first slot in the file.
start a loop
GET a record from the file (from slot "counter")
check to see whether the record has a title
if it does, print it to screen
advance to the next record (increase the "counter" variable by 1)
end the loop when the End Of File #1 is reached( When EOF(1) is true )
================================================
Okay, I feel like this is the most help that I can give you short of writing the actual program for you, which I would feel wrong doing since this is a school assignment.
However, if you post your code, I will go through it and make suggestions.
Good luck, and I hope this tutorial helps you make a successful program!
*peace*
Meg.