2

I recently started work on a project that searches a CSV file for duplicate entries and present the user the option to delete one or both entries.

Simple enough it would seem, however I am having an issue with the function that actually parses the CSV file into memory.

Here is the code in question...

using System;
using System.IO;
using Microsoft.VisualBasic.FileIO;
using System.Collections.Generic;
using System.Windows.Forms;


public List<string[]> parseCSV(string path)
{
    List<string[]> parsedData = new List<string[]>();
    string[] fields;

    TextFieldParser parser = null;
    string line = parser.ReadLine();

    try
    {
        /*TextFieldParser*/ parser = new TextFieldParser(@"c:\temp\test.csv");
        parser.TextFieldType = FieldType.Delimited;
        parser.SetDelimiters(",");

        while (!parser.EndOfData)
        {
            fields = parser.ReadFields();
            parsedData.Add(fields);

            //Did more stuff here with each field.
        }

        parser.Close();

    }
    catch (Exception e)
    {
        MessageBox.Show(e.Message);
    }

   return parsedData;
}

For some reason in VS2017 parseCSV is underlined in red in the function declaration. I can't figure out why this is. I've tried the obvious fixes such as changing the function name from parseCSV to something else but that obviously didn't.

5
  • 2
    What is the error? Commented Nov 13, 2018 at 23:53
  • I do not get a red underline, just a complaint about naming conventions. Commented Nov 14, 2018 at 0:11
  • If you hover over the squiggly line, you can see an error. What is it? We're not too good at guessing things like that. Is it only that it wants you to rename ParseCSV to ParseCsv? If so, ignore it (the latter is the preferred name by most conventions). Are you getting any errors or significant warnings? Commented Nov 14, 2018 at 0:26
  • 2
    You should know that this has all been done before: there are oodles of CSV Parsers which do fabulous things (better than that VB thing). See CSVHelper for instance Commented Nov 14, 2018 at 0:30
  • 1
    When I hover the cursor over the function name I get an error saying "List<string[]> <invalid-gloabal-code>.ParseCSV(string path) a namespace cannot directly contain members such as fields or methods". As for Disaffected's comment. I'm aware there are a million CSV parsers out there that are much better than mine, but like I said before this is mainly a learning project. If this where something I was writing in a professional setting I would absolutely use a better alternative. Commented Nov 14, 2018 at 0:43

1 Answer 1

2

In C# everything is contained in a class, you can't just declare a method within a namespace directly.

using System;
using System.IO;
using Microsoft.VisualBasic.FileIO;
using System.Collections.Generic;
using System.Windows.Forms;

class MyLearningOnlyCsvParser {

  public List<Customer_Data> parseCSV(string path)
  {
    List<Customer_Data> parsedData = new List<Customer_Data>();
    string[] fields;

    TextFieldParser parser = null;
    string line = parser.ReadLine();

    try
    {
        /*TextFieldParser*/ parser = new TextFieldParser(@"c:\temp\test.csv");
        parser.TextFieldType = FieldType.Delimited;
        parser.SetDelimiters(",");

        while (!parser.EndOfData)
        {
            fields = parser.ReadFields();

            // assume the CSV is always with  11 columns
            if(fields.length == 11) {
                Customer_Data newData = new Customer_Data();
                newData.name = fields[0];
                newData.company = fields[1];
                // assign to the rest of the customer data properties with each fields
                parsedData.Add(newData);
            }
            else {
                // error handling of not well formed CSV
            }

        }

        parser.Close();

    }
    catch (Exception e)
    {
        MessageBox.Show(e.Message);
    }

   return parsedData;
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Ok thanks for the tip. Historically I've worked primarily with C++ so some of this is new to me.
So here's where I'm at so Far.

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.