c# - Unexpected results with StreamReader.ReadLine() -
i testing application stores user's contact details in file. information stored in local compact database primary method - storing details in file backup in case details lost.
the file i'm using testing has personal data in it, hope understand have replaced lines placeholders! structure of file follows (minus first line):
file: business name mr. joe bloggs user@email.com address line 1 address line 2 city postcode country 07777123456
below, have code reads file , stores each line variable. structure of file never change, hence simple code:
public static bool restorebusinesstable(out string title, out string busname, out string mobilenumber, out string firstname, out string lastname) { string email = "", referral = "", contactno, addressline1 = "", addressline2 = "", city = "", postcode = "", country = "", district = ""; busname = null; mobilenumber = null; firstname = null; lastname = null; title = null; try { if (!file.exists(filename)) return false; streamreader sr = new streamreader(filename); string work; work = sr.readline(); // empty line work = sr.readline(); // empty line busname = sr.readline(); title = sr.readline(); firstname = sr.readline(); lastname = sr.readline(); email = sr.readline(); referral = sr.readline(); addressline1 = sr.readline(); addressline2 = sr.readline(); city = sr.readline(); postcode = sr.readline(); country = sr.readline(); work = sr.readline(); // empty line work = sr.readline(); // empty line contactno = sr.readline(); district = sr.readline(); mobilenumber = sr.readline(); sr.close(); // add database here return true; } catch { return false; } }
when running code, noticed busname
, title
, firstname
, , lastname
had value of 07777123456
. data looked this:
07777123456 07777123456 07777123456 07777123456 user@email.com address line 1 address line 2 city postcode country 07777123456
i not have asynchronous processes or threads writing file @ same time. shed light happening here, , why first 4 lines appear mobile number?
one way calling code supply same variable's/field's address various out
parameters:
string tmp; restorebusinesstable(out tmp, out tmp, out tmp, ...);
here same address passed in each location, no matter whether code assigns title
, busname
, etc writing same actual location.
since mobilenumber
assigned last, value assigned mobile number value appears values.
the key point here title
, busname
, etc not each reference string - because of out
(or ref
, equally) each reference reference string.
Comments
Post a Comment