vb.net - Parse txt file using a list of objects -


i trying parse .txt file has multiple loans in it. logic is: create loan class properties need, create list of loan objects. create new loan object , add list. read through txt file , fill objects properties. when reach end of file need create new loan object , start on because file has multiple loans in , need 1 object per loan. problem when use code below error "local variable 'myloans' hides variable in enclosing block". there better way this?

    public class loan         public property loanid string         public property loanprovider string     end class      dim listofloans new list(of loan)()     dim myloans new loan     listofloans.add(myloans)      dim line string     using r new streamreader("c:text.txt")         line = r.readline()         while (not line nothing)             if (line.substring(0, 10) = "loan id:")                 myloans.loanid = line.substring(10, line.length - 10).trim()              elseif (line.substring(0, 14) = "loan provider:")                 myloans.loanprovider = line.substring(14, line.length - 19).trim()              elseif (line.substring(0, 30) = "last line of file:")                 dim myloans new loan                 listofloans.add(myloans)              end if             line = r.readline         loop     end using 

thanks in advance!

in these lines creating new instance of class loan , assign new instance different variable called same name of external variable.
of course lifetime of variable limited enclosing elseif/end if block , has nothing first one. name , compiler warns of pitfall

elseif (line.substring(0, 30) = "last line of file:")     dim myloans new loan     listofloans.add(myloans) end if 

you should change line in

elseif (line.substring(0, 30) = "last line of file:")     myloans = new loan    'no dim myloans external one'     listofloans.add(myloans) end if 

this create new instance of loan class , assign reference same variable declared @ start


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -