c# Help making values global -


so understand how make global values , fact 1. shouldn't , 2. cannot use value created in different "context" however, i'm not sure how correct problem in case. think make sense if read code

            //read in load query testcsv         var sourcepath = @"d:\\load query test.csv"; //what inital csv         var delimiter = ",";         var firstlinecontainsheaders = true; //csv has headers         //creates temp file takes less time loading memory         var temppath = path.combine(@"d:", path.getrandomfilename());         var linenumber = 0;          var splitexpression = new regex(@"(" + delimiter + @")(?=(?:[^""]|""[^""]*"")*$)");          using (var writer = new streamwriter(temppath))         using (var reader = new streamreader(sourcepath))         {             string line = null;             string[] headers = null;             if (firstlinecontainsheaders)             {                 line = reader.readline();                 linenumber++;                  if (string.isnullorempty(line)) return; // file empty;                  headers = splitexpression.split(line).where(s => s != delimiter).toarray();                  writer.writeline(line); // write original header temp file.             }             var = 0; //used in 2nd while loop later             string lines = null;//used in next using statement             while ((line = reader.readline()) != null)             {                 linenumber++;                  var columns = splitexpression.split(line).where(s => s != delimiter).toarray();                  //make sure have same number of columns in line                 if (headers == null) headers = new string[columns.length];                  if (columns.length != headers.length) throw new invalidoperationexception(string.format("line {0} missing 1 or more columns.", linenumber));                 string baddate = "date entered incorrectly";                                    //used in next while loop                 // while loop read in user input datetime , use column pi server.                 //if date time entered incorrectly tell user try input datetime again                 while (i==0)                 {                     console.writeline("enter date, ex:16 jun 8:30 2008, press enter when done"); //instruct user in how enter date                     string userinput = console.readline(); //read in date user enters                     string format = "dd mmm h:mm tt yyyy"; //how system read date entered                      datetime datetime;                      //if date entered correctly, parse it, grab parsed value datetime , exit loop                     if (datetime.tryparseexact(userinput, format, cultureinfo.invariantculture, datetimestyles.none, out datetime))                     {                                                    = 1; //set flag exit while loop                     }                     //if input bad return "date entered incorrectly , run loop again                     else                     {                         console.writeline(baddate);                         i=0; //set flag run loop again                     }                 }                 var del = ",";                                                                  //used in next using statement                 var splitexpression = new regex(@"(" + del + @")(?=(?:[^""]|""[^""]*"")*$)");   //used in next using statement                 //use datetime previous while loop , use add each point in "testpts.csv" "load query test.csv"                 using (streamreader tags = new streamreader(@"d:\\testpts.csv"))                 {                    // string userinput = console.readline();                     string format = "dd mmm h:mm tt yyyy";                     datetime.tryparseexact(userinput, format, cultureinfo.invariantculture, datetimestyles.none, out datetime);                     lines = tags.readline();                     var columns1 = splitexpression.split(lines).where(s => s != del).toarray();                     var point = pipoint.findpipoint(piserver, lines);                     var value = point.recordedvalue(datetime);                     string returnxml = string.format(@"<value=""{0}"" />", value);                     columns[15] = columns[15].replace("0", returnxml); //column point should placed in (in load query test.csv)                 }                 //if statement replace 0 testpt values column 13 values                 if (columns[15].contains("0"))                 {                     columns[15] = columns[15].replace("0", columns[13]);                 }                  writer.writeline(string.join(delimiter, columns));             }          }          file.delete(sourcepath); //delete original csv         file.move(temppath, sourcepath); //replace old csv edited 1          console.readline(); 

i'm getting error in using statement:

using (streamreader tags = new streamreader(@"d:\\testpts.csv"))                 {                    // string userinput = console.readline();                     string format = "dd mmm h:mm tt yyyy";                     datetime.tryparseexact(userinput, format, cultureinfo.invariantculture, datetimestyles.none, out datetime);                     lines = tags.readline();                     var columns1 = splitexpression.split(lines).where(s => s != del).toarray();                     var point = pipoint.findpipoint(piserver, lines);                     var value = point.recordedvalue(datetime);                     string returnxml = string.format(@"<value=""{0}"" />", value);                     columns[15] = columns[15].replace("0", returnxml); //column point should placed in (in load query test.csv)                 } 

in case datetime , userinput values out of context. need them created in previous while loop because want user able enter correct date once , ensure entered correctly make sure script pull data.

please let me know if there way can order code or how can make userinput , datetime global. thank you

your problem lies in "datetime" variable. "userinput" fine, inner using statement has access scope of outer using statement, because inner 1 part of outer's scope.

the problem "datetime" - variable declared inside while loop, , there using block afterwards - after variable not available anymore, because scope disposed - references non existent variable.

solution: move declaration of datetime variable out of while. say, line before while's definition.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -