0

I have a c# lab question:

This is my code todo add data from the csv file, after compile it gives a error the name "rows" does not exist in current content

foreach (string row in rows)
{
    if (string.IsNullOrEmpty(row)) continue;
    string[] cols = row.Split(',');
    DailyValues v = new DailyValues();

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


    return values;
}    
2
  • I'm sure it doesn't even compile. For more robust solution have a look at LINQtoCSV library. Commented Nov 9, 2012 at 3:22
  • I have edited your title. Please see, "Should questions include “tags” in their titles?", where the consensus is "no, they should not". Commented Nov 9, 2012 at 5:01

1 Answer 1

1

It looks like your CSV file has data which can't be converted into a Decimal. Run it in the debugger, and have a look at row when the exception is thrown.

If you use Decimal.TryParse(), the return value will tell you if the conversion was successful without an exception being thrown.

Edit:

As an example for TryParse:

Decimal _Open, _High;
if (!Decimal.TryParse(cols[0], out _Open))
{
    Debug.Print("Error on row: {0}", row);
    continue;
}
v.Open = _Open;

if (!Decimal.TryParse(cols[1], out _High))
{
    Debug.Print("Error on row: {0}", row);
    continue;
}
v.High = _High;
Sign up to request clarification or add additional context in comments.

11 Comments

my csv file has 4924 of data and the task is require to add all the data into the list, and from the data:Find the highest and the lowest amount the stock ever traded for and display the values together with the corresponding dates, :Calculate the average volume traded per day, :Calculate the average volume traded for each of the calendar years. And after i replace with the Decimal.TryParse when i debug it shows 1 error: the name "row" does not exist in current content
It should work - Convert.ToDecimal uses Decimal.Parse() internally, and TryParse is the same thing with more checks. The other option is to surround every Convert.To*() call with try { ... } catch { throw; } and put a breakpoint on the throw; line. This is the slower and uglier method though.
I am not good with try and catch block, can you show me a example?
If it's something you haven't covered yet in your course, you probably shouldn't use it in a lab. Otherwise, just replace the ... from my comment with the Convert.To*() calls and add new lines as appropriate around the braces. You could also use one block to surround all of them which will give you the line if not the individual value.
add new lines, you mean a console.writeline()? And i compile again it still has the error showing rows not exist in current content
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.