Writing CSV Parser in C#












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:temptest.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.










share|improve this question




















  • 2





    What is the error?

    – MikeH
    Nov 13 '18 at 23:53











  • I do not get a red underline, just a complaint about naming conventions.

    – Kyle Huff
    Nov 14 '18 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?

    – Flydog57
    Nov 14 '18 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

    – None of the Above
    Nov 14 '18 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.

    – user3010628
    Nov 14 '18 at 0:43


















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:temptest.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.










share|improve this question




















  • 2





    What is the error?

    – MikeH
    Nov 13 '18 at 23:53











  • I do not get a red underline, just a complaint about naming conventions.

    – Kyle Huff
    Nov 14 '18 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?

    – Flydog57
    Nov 14 '18 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

    – None of the Above
    Nov 14 '18 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.

    – user3010628
    Nov 14 '18 at 0:43
















2












2








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:temptest.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.










share|improve this question
















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:temptest.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.







c# csv parsing






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 18:14









JohnLBevan

14.4k146107




14.4k146107










asked Nov 13 '18 at 23:52









user3010628user3010628

141




141








  • 2





    What is the error?

    – MikeH
    Nov 13 '18 at 23:53











  • I do not get a red underline, just a complaint about naming conventions.

    – Kyle Huff
    Nov 14 '18 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?

    – Flydog57
    Nov 14 '18 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

    – None of the Above
    Nov 14 '18 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.

    – user3010628
    Nov 14 '18 at 0:43
















  • 2





    What is the error?

    – MikeH
    Nov 13 '18 at 23:53











  • I do not get a red underline, just a complaint about naming conventions.

    – Kyle Huff
    Nov 14 '18 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?

    – Flydog57
    Nov 14 '18 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

    – None of the Above
    Nov 14 '18 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.

    – user3010628
    Nov 14 '18 at 0:43










2




2





What is the error?

– MikeH
Nov 13 '18 at 23:53





What is the error?

– MikeH
Nov 13 '18 at 23:53













I do not get a red underline, just a complaint about naming conventions.

– Kyle Huff
Nov 14 '18 at 0:11





I do not get a red underline, just a complaint about naming conventions.

– Kyle Huff
Nov 14 '18 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?

– Flydog57
Nov 14 '18 at 0:26





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?

– Flydog57
Nov 14 '18 at 0:26




2




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

– None of the Above
Nov 14 '18 at 0:30





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

– None of the Above
Nov 14 '18 at 0:30




1




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.

– user3010628
Nov 14 '18 at 0:43







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.

– user3010628
Nov 14 '18 at 0:43














1 Answer
1






active

oldest

votes


















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:temptest.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;
}
}





share|improve this answer


























  • Ok thanks for the tip. Historically I've worked primarily with C++ so some of this is new to me.

    – user3010628
    Nov 14 '18 at 13:04











  • So here's where I'm at so Far.

    – user3010628
    Nov 14 '18 at 13:05











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53291170%2fwriting-csv-parser-in-c-sharp%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









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:temptest.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;
}
}





share|improve this answer


























  • Ok thanks for the tip. Historically I've worked primarily with C++ so some of this is new to me.

    – user3010628
    Nov 14 '18 at 13:04











  • So here's where I'm at so Far.

    – user3010628
    Nov 14 '18 at 13:05
















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:temptest.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;
}
}





share|improve this answer


























  • Ok thanks for the tip. Historically I've worked primarily with C++ so some of this is new to me.

    – user3010628
    Nov 14 '18 at 13:04











  • So here's where I'm at so Far.

    – user3010628
    Nov 14 '18 at 13:05














2












2








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:temptest.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;
}
}





share|improve this answer















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:temptest.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;
}
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 15 '18 at 18:11

























answered Nov 14 '18 at 1:29









techherotechhero

752810




752810













  • Ok thanks for the tip. Historically I've worked primarily with C++ so some of this is new to me.

    – user3010628
    Nov 14 '18 at 13:04











  • So here's where I'm at so Far.

    – user3010628
    Nov 14 '18 at 13:05



















  • Ok thanks for the tip. Historically I've worked primarily with C++ so some of this is new to me.

    – user3010628
    Nov 14 '18 at 13:04











  • So here's where I'm at so Far.

    – user3010628
    Nov 14 '18 at 13:05

















Ok thanks for the tip. Historically I've worked primarily with C++ so some of this is new to me.

– user3010628
Nov 14 '18 at 13:04





Ok thanks for the tip. Historically I've worked primarily with C++ so some of this is new to me.

– user3010628
Nov 14 '18 at 13:04













So here's where I'm at so Far.

– user3010628
Nov 14 '18 at 13:05





So here's where I'm at so Far.

– user3010628
Nov 14 '18 at 13:05


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53291170%2fwriting-csv-parser-in-c-sharp%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Florida Star v. B. J. F.

Danny Elfman

Lugert, Oklahoma