Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a csv file and i need to read the csv file and add data from the csv. when i compile it gave me error:
Error 1 The name 'CreateValues' does not exist in the current context

Error 2 Operator '!=' cannot be applied to operands of type 'ReadCsvFile.DailyValues' and null

Error 3 The name 'rows' does not exist in the current context



C#
 // reads stock values from csv file into a List
 public static List<DailyValues> GetStockValues(string filePath)
        {
            List<DailyValues> values = new List<DailyValues>();
          
            using (StreamReader reader = new StreamReader(filePath))
            {
                string line;
                DailyValues v;
                while ((line = reader.ReadLine()) != null)
                {
                    v = CreateValues(line);
                    if (v != null)
                        values.Add(v);
                }
            }
            return values;
// TODO: add the data from the csv file
            foreach (string row in rows)
            {
                if (string.IsNullOrEmpty(row)) continue;
                string[] cols = row.Split(',');
                DailyValues v = new DailyValues();

                v.Open = Convert.ToDecimal(cols[0]);
                v.High = Convert.ToDecimal(cols[1]);
                v.Low = Convert.ToDecimal(cols[2]);
                v.Close = Convert.ToDecimal (cols[3]);
                v.Volume = Convert.ToDecimal (cols[4]);
                v.AdjClose = Convert.ToDecimal(cols[5]);
                v.Date = Convert.ToDateTime(cols[6]);
                values.Add(v);


                return values;
Posted

>>Error 1 The name 'CreateValues' does not exist in the current context

Does CreateValues function exists. If it does, is it static. If not, either make it static or remove static from GetStockValues.

>>Error 2 Operator '!=' cannot be applied to operands of type 'ReadCsvFile.DailyValues' and null

Is DailyValues struct ? You cannot apply null check to struct.
Change following
C#
if (v != null)

to
C#
if (v != default(DailyValues))


>>Error 3 The name 'rows' does not exist in the current context
Where are rows ? It is missing. The error is self explanatory. Add a variable rows (which should have IEnumerable implemented on data type (some list/array is simple words))
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900