vb.net - Use streamreader to load data from text file into textboxes, code cannot find objStudent array -
option strict on imports system.text.regularexpressions imports system.io public class studenttestscores private structure student dim strstudentname string dim dbltestscores() double dim dblaverage double end structure public function getdoubletestscore(byval value string) double 'checks if value numeric , returns message if error found if isnumeric(value) dim dblvalue = cdbl(value) 'check make sure number positive , less or equal 100 if dblvalue >= 0 , dblvalue <= 100 return dblvalue else throw new exception("the number needs between 0 , 100") end if else throw new exception("please enter number in test score area.") end if end function private sub btncalc_click(sender object, e eventargs) handles btncalc.click 'creates variable , runs isvalidname dim objstudent(6) student if isvalidname() = true try ' initializes each of test score arrays in student object integer = 0 5 initializetestscores(objstudent(i)) next initializetestscores(objstudent(0)) 'runs isnumeric function txtstudentscores objstudent(0).dbltestscores(0) = getdoubletestscore(txtstudent1score1.text) objstudent(0).dbltestscores(1) = getdoubletestscore(txtstudent1score2.text) objstudent(0).dbltestscores(2) = getdoubletestscore(txtstudent1score3.text) objstudent(0).dbltestscores(3) = getdoubletestscore(txtstudent1score4.text) objstudent(0).dbltestscores(4) = getdoubletestscore(txtstudent1score5.text) objstudent(1).dbltestscores(0) = getdoubletestscore(txtstudent2score1.text) objstudent(1).dbltestscores(1) = getdoubletestscore(txtstudent2score2.text) objstudent(1).dbltestscores(2) = getdoubletestscore(txtstudent2score3.text) objstudent(1).dbltestscores(3) = getdoubletestscore(txtstudent2score4.text) objstudent(1).dbltestscores(4) = getdoubletestscore(txtstudent2score5.text) objstudent(2).dbltestscores(0) = getdoubletestscore(txtstudent3score1.text) objstudent(2).dbltestscores(1) = getdoubletestscore(txtstudent3score2.text) objstudent(2).dbltestscores(2) = getdoubletestscore(txtstudent3score3.text) objstudent(2).dbltestscores(3) = getdoubletestscore(txtstudent3score4.text) objstudent(2).dbltestscores(4) = getdoubletestscore(txtstudent3score5.text) objstudent(3).dbltestscores(0) = getdoubletestscore(txtstudent4score1.text) objstudent(3).dbltestscores(1) = getdoubletestscore(txtstudent4score2.text) objstudent(3).dbltestscores(2) = getdoubletestscore(txtstudent4score3.text) objstudent(3).dbltestscores(3) = getdoubletestscore(txtstudent4score4.text) objstudent(3).dbltestscores(4) = getdoubletestscore(txtstudent4score5.text) objstudent(4).dbltestscores(0) = getdoubletestscore(txtstudent5score1.text) objstudent(4).dbltestscores(1) = getdoubletestscore(txtstudent5score2.text) objstudent(4).dbltestscores(2) = getdoubletestscore(txtstudent5score3.text) objstudent(4).dbltestscores(3) = getdoubletestscore(txtstudent5score4.text) objstudent(4).dbltestscores(4) = getdoubletestscore(txtstudent5score5.text) objstudent(5).dbltestscores(0) = getdoubletestscore(txtstudent6score1.text) objstudent(5).dbltestscores(1) = getdoubletestscore(txtstudent6score2.text) objstudent(5).dbltestscores(2) = getdoubletestscore(txtstudent6score3.text) objstudent(5).dbltestscores(3) = getdoubletestscore(txtstudent6score4.text) objstudent(5).dbltestscores(4) = getdoubletestscore(txtstudent6score5.text) ' loops through each student structure object , calculates average test score. integer = 0 5 objstudent(i).dblaverage = caculatestudentaverage(objstudent(i)) next objstudent(0).strstudentname = txtstudent1.text objstudent(1).strstudentname = txtstudent2.text objstudent(2).strstudentname = txtstudent3.text objstudent(3).strstudentname = txtstudent4.text objstudent(4).strstudentname = txtstudent5.text objstudent(5).strstudentname = txtstudent6.text lblaveragestudent1.text = objstudent(0).dblaverage.tostring() lblaveragestudent2.text = objstudent(1).dblaverage.tostring() lblaveragestudent3.text = objstudent(2).dblaverage.tostring() lblaveragestudent4.text = objstudent(3).dblaverage.tostring() lblaveragestudent5.text = objstudent(4).dblaverage.tostring() lblaveragestudent6.text = objstudent(5).dblaverage.tostring() 'this creates text file program write dim studentfile system.io.streamwriter dim strfilename string = "studenttestscore.txt" studentfile = system.io.file.appendtext(strfilename) 'creates loop takes 6 students integer = 0 5 studentfile.write("student name: ") studentfile.write(objstudent(i).strstudentname) studentfile.write(" student test scores: ") 'this creates loop students , test scores intindex2 integer = 0 4 studentfile.write(objstudent(i).dbltestscores(intindex2).tostring()) if intindex2 <> 4 studentfile.write(", ") end if 'finally average ran using objstudent (i) next studentfile.write(" average score = ") studentfile.write(objstudent(i).dblaverage.tostring()) studentfile.writeline() next 'closes text file created studentfile.close() 'shows message box says file written text file , or modified messagebox.show("student test score file created or modified.") catch ex exception messagebox.show(ex.message) end try end if end sub private sub initializetestscores(byref objstudent student) 'references objstudent object ' takes array dbltestscores , makes fixed array of size 6 since not given number in structure redim objstudent.dbltestscores(5) end sub private function caculatestudentaverage(byval objstudent student) double ' loop loops through each value in dbltestscores , adds them objstudent integer = 0 4 objstudent.dblaverage += objstudent.dbltestscores(i) next ' divides , stores variable objstudent.dblaverage /= 5 'returns student average return objstudent.dblaverage end function private sub loadtoolstripmenuitem_click(sender object, e eventargs) handles loadtoolstripmenuitem.click ' create new open file dialog dim myfiledialog new system.windows.forms.openfiledialog ' configure dialog show text files ' set title , set filename field blank moment. myfiledialog.filename = "studenttestscore.txt" ' show dialog , see if user pressed ok. if myfiledialog.showdialog() = windows.forms.dialogresult.ok ' check see if selected file , exists. if file.exists(myfiledialog.filename) dim strfile string = myfiledialog.filename dim reader streamreader try ' setup file stream reader read text file. reader = new streamreader(new filestream(strfile, filemode.open, fileaccess.read)) ' while there data read, read each line rich edit box control. select case 0 txtstudent1.text = student.objstudent(0) txtstudent1score1.text = end select while reader.peek > -1 txtstudent1.text &= reader.readline() end while ' close file reader.close() catch ex filenotfoundexception ' if file not found, tell user. messagebox.show("file not found. please try again.") end try end if end if end sub end class
Comments
Post a Comment