Planning - One Dimensional Array and Files

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
morning everybody this is then planning example two of unit four lesson one we are working with arrays and in this example we're going to have a look at the following first of all program 94 we're going to see how we're going to work with a program that's got to use a one-dimensional array to store data this data is read and written to a file i am then going to discuss two topics of program 96 and that would be the form load event procedure for the main menu and then the form closing event procedure again for the main menu so i'll do that in the end but first of all program 94. if we go have a look at program 94 then we're again going to use a one-dimensional array to save names and read names if you have a good look at it then you'll see that you need one screen called the add names form that's going to take a name from the user text box and we're going to save that in an array and then we're going to have another form view names form this form is going to have a list box to display some data and then it's got three different buttons view names to view the names currently stored in memory in the array read names to read names from a file and load it in the array and store names to take whatever names are in a file or in memory and save that to a file so the processing of this program is not optimum i would if i'm going to give something to the user one day rather have a look at something like program 96 but baby steps we need to first learn how to read names from a file and load it into an array and then take array data and save it to a file so we need to know how to do each of these operations if i then open something like an example screen then the ad name section is real real easy you've written data to a one-dimensional array before so the processing of this screen rather simple if i have a look at view names i've got a list box here the button view names the processing thereof should also be simple to you it simply takes all the data that's currently stored in the array and it saves it on into the list box it displays it onto the list box the processing of the button read all names from file and the processing of the button store all names to file this is new and this is then what we are going to have a look at so make sure that you understand exactly what we need to do and that you've got a good user interface in mind if i now go have a look at the program so as said before we need to basically save names in a one-dimensional array and then read and write the array to a file then at module level i will first of all need a file name like i explained in the previous video in order to use the exact same name all the time better to define a constant and then we need everything that we learned about in arrays so i've got a maximum i've got 500 names to store i've got an array for my names and i've got an array counter to tell me how many items i've got at this stage saved so form ad names as i said before should be general knowledge at this stage to you so i will test if the array has got space if it's got space i'm going to get the name from the text box store it in the next array element increment the route counter i'm going to tell the user the name is saved and then i'm going to read the form for the next input if the array doesn't have space i'm going to display an error message so once you've coded this program a real nice way of testing if this program really works is to change the maximum if you change it to something like five it's easy to see whether you can handle five names whether your program functions correctly okay so one of the benefits of using a constant for the number of if i then scroll down a little bit go have a look at form view names we've got a show names procedure that just displays all the names in the list box again also something you've coded before and the button event procedure simply calls the show all procedure so nothing new there over here i've got new processing button read names must read the names from a file and load it at the end of whatever existing data i might have in the array so if the user just opens the program right at the start and then first read then real nice we've got some names to start with so let's have a look at the processing steps so declare the variables rather straightforward before i read i always need to test if the file is present i will obviously they need to open the file to read i've got a loop until end of file i'm going to read the line but before i save that line i first need to test do i have space in the array if it's true i'm going to add the name at the end and increment the array counter otherwise if there's no space in the array i'm just simply going to tell the user no sorry the array is full after the loop i always close a file i can go display all the names that i've got and if the file wasn't present i display an error message so um this show names to display the array i'd rather go and do that over here in the program display the names i'm going to call the show names procedure like that on the right hand side i've listed the important file operation so to test if a file is present there's an exists method saved in the file class and the file class is saved in the i o package so important that we understand those type of things quickly tell you if i have something like this a namespace in the vb.net environment to sort of like our surnames the clause is sort of like a name of a person and then a method is sort of like an action that we're going to perform so if i want to ask emma motlong to run so in human terms i would say emma matlong run please because we've got madness we can say please now in programming terms i'm first going to start with the surname then the name of the clause and then the action so that's now programming terms to say what action i want i want to run who must run emma where will i find emma in the mod long family okay so exactly the same appears over here i've got the i o name space so that's the i o input output family all the operations dealing with input and output would be stored in this namespace in the namespace i've got a file clause and that file clause have got an exists method fix this method takes a file name and this method gives me back a boolean it's a function tells me true or false and i'm directly i can use this boolean in the if statement so that whole thing gives me a true or false i can't say i want to test if the result is returned is a true but um more advanced programmers will not really use that i'm going to leave it here for now i'll maybe just highlight it okay so we need to know what this line means don't just copy it with my heart in the io name namespace there's a file clause the file clause have got an exists method it takes a string the file name as a parameter and that gives us back a true or false i can test in an if statement if the file exists or not if i want to open the file for input i will do it like this info in this case would be a local variable of the type stream reader so dem info stream reader or io dot stream reader actually would appear where i define the variables okay in files of the type stream reader stream reader again another clause that's coded and it's io.streamreader because the io namespace is used for everything that stools files now i've got a loop do while the input file doesn't give me a minus one when i call the peak function so peak is a function in the class stream reader y in the cloud stream reader because i've created info of the type stream reader so i know peak would be a function in that class readline is also a function in that class it reads our line and i'm storing the data in name now can't just save the data in an array we always need to check before i use an array do i have space if it's a yes save the name increment the array counter nice the messages say the ray is full i will give a message if there is no fall a nice message and i need to remember to always close the file really when you want to use a file open loop to read close the file do i have all of that and obviously inside the loop i will need to read the line if i have a look at the button store names so here when the user clicks on the button we need to save everything that's an array to a file so i will need to open the file for create mode that was specified in the question in the specifications i will need to traverse my array so as always sorry from zero to count minus one i will write the name to the file and after the loop close the file my statement should look something like this so open the file this will create the out file object loop to traverse the array call the right line method and what will i write my counter we'll traverse the array 012 etc so every name that's saved and close the file right at the end so now if i am to run this program there we go around the program if i go have a look at view names click on view names then nothing initially in the file or in the array i start with blank names first time that i run the program so if i am going to save some data i first need to know what i want to do so if i go have a look at some test data then write it distort i want to add a few names let's add emma and sierra then i want to view and iman 0 would be displayed in the list box this is still just in memory if i go add a name let's just go for bin if i go view again then bin would be displayed as well if i now go right to file then this file would be created for me so now if i stop the application and start it again just to clear the memory if i'm immediately going to read from file and then choose view names then i should see emma sierra bin if my application works perfectly fine so let's go have a look i am just going to maybe minimize this a little bit so that i can see my t-state as well okay no data added initially so let's go for emma and then for sarah if i close that go to view names not displayed immediately i first need to click on the button there i've got iman sarah so if i go add another name we add bin and if i click on view names now i've got emma sierra and ben there we go if i now go and click store all the names to the file or write all names i get a confirmation it's saved to a file message that i display so if i go and exit the application it's perfectly fine if i run it again so yeah i just started the program if i go click on view names and right at the start click on the view names button nothing is displayed we just started the program no data and memory so if i scroll down a little bit i did stop the application and start it again so that our memory was cleared if i now go click on read all names and voila view names schooled automatically emma sarah bin is saved so if i'm just going to add one more name let's go for nuku if i go click on view names it's all fine if i close the program i didn't save to a file and then i start the program again if i go to view names and i read the names from a file why me the user i didn't click on this store the names to a file right at the end so if i do so let's go save in the array go to view names then save the names to a file and now if i stop the application and i start it again if i read the names from the file then only is nuku saved okay so my program save the data in the right to file again read the data from the file those two but at this stage it's operations that the user must perform manually and that brings me to program 96 i don't want the user to have to click on a button to save the data in a file and i don't want the user to need to go click on a button to read the data from a file so in the main menu if i can read from a file at the start of the program that's done in the form load event procedure it would be terrific and then as soon as the user closes the program i want to save the data automatically to a file okay so that i don't need the user to manually read from a file and save to a file like program 94 does user is going to forget to write okay i don't want that so program 94 asks you to read the data from the file if the file exists in the form load even procedure so you might ask me what's the form load event procedure and here i'm gonna tell you oh boy guess what it's something that you are all too familiar with sorry okay to the solution explorer in the main menu program if i double click on the form what opens an event procedure called load so this event procedure is called once at the start of the program so at the start of the main menu okay because i have clicked on full main menu and this would now be where i will read from a file all those operations that you just did dcf25 is the loop until end of file this would be the best space or place to do so how do i get to now the other event procedure form load is easy you've done it plenty times in the past by accident double click on the form formload event procedure opens to close the file or to close to select the event procedure that's called when the file is closed or the application is closed automatically a little bit more complicated so in vb these two check boxes right at the bottom of the um design screen the the code screen where i code my statements if i go choose the leftmost checkbox and i go to four main menu events and i choose the rightmost checkbox form load is coded that's why it's highlighted um i always need to go just look for the thing it's just called closing in java it's called form closing in vb be careful there's a form closed past tense and a form closing now remember kfc zinger wings we all love them you always want to go pick the zing option closing if i click there the form closing event procedures automatically selected and this is called once at the end of the program your application so when the menu is closed doesn't matter how whether it's via the file option or the windows close button and this is where i'm going to write to normally a new file so you're going to create the file use create mode to save all the data okay the specifications per program can differ but under normal circumstances we would do these two things in the planning what would be the event type form closing or form load to read or to write and the name the form name and load and closing okay so it's only the what is the event and what is the name of the event procedure that's different rest of the statements exactly the same okay so i hope you've got have fun hope you guys manage with the programs thank you
Info
Channel: BeginnerProgramming
Views: 188
Rating: undefined out of 5
Keywords:
Id: pdGL3zJdAmc
Channel Id: undefined
Length: 25min 38sec (1538 seconds)
Published: Wed Nov 10 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.