0
votes

Screenshot of error placementI am using Visual Studio 2013, .NET 4.5.2, OpenXml 2.5 on Windows 7 Enterprise. I created a snippet of code to import two .csv files into a pair of generic lists and to compare them for exceptions. When I went to build a generic class to load the .csv files, TextFieldParser would not work. It works within Main(), but not within a class.

Although I suspect the answer has something to do with "using alias within a namespace", it works perfectly within Main(). Microsoft has a disclaimer that the rhs cannot be a generic type, but if it works in one place, why not the other?

Is this expected behavior or is there a known workaround?

BACKGROUND: TextFieldParser is a VB.NET class that neatly handles parsing of .csv or fixed field files. Generic lists, or List, are list classes that can handle anything placed in them. In my case, I am using a record class as the object contained. OpenXml is a Microsoft technology for working with Office products to deal with XML config files or connection strings. I will be importing config settings to make the classes used generic. It is in trying to build a generic class for the TextFieldParser where I am having issues. Google has gotten me this far, but no farther.

The working code in Main():

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualBasic.FileIO;
using ExportToExcel;

    public static void Main(string[] args)
    {
        // HARD_CODED FOR EXAMPLE ONLY - TO BE RETRIEVED FROM APP.CONFIG IN REAL PROGRAM
        string hospPath = @"C:\\events\\inbound\\OBLEN_COB_Active_Inv_Advi_Daily_.csv";
        string vendPath = @"C:\\events\\outbound\\Advi_OBlen_Active_Inv_Ack_Daily_.csv";

        List<DenialRecord> hospList = new List<DenialRecord>();
        List<DenialRecord> vendList = new List<DenialRecord>();

        using (TextFieldParser hospParser = new Microsoft.VisualBasic.FileIO.TextFieldParser(hospPath))
        {
            hospParser.TextFieldType = FieldType.Delimited;
            hospParser.SetDelimiters(",");
            hospParser.HasFieldsEnclosedInQuotes = false;
            hospParser.TrimWhiteSpace = true;

            while (!hospParser.EndOfData)
            {

                try
                {
                    string[] row = hospParser.ReadFields();
                    if (row.Length <= 7)
                    {
                        DenialRecord dr = new DenialRecord(row[0], row[1], row[2], row[3], row[4], row[5], row[6]);
                        hospList.Add(dr);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("Error is:  {0}", e.ToString());
                }
            }
            hospParser.Close();
            hospParser.Dispose();
        }

The non working code in a class by itself:

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualBasic.FileIO;
using ExportToExcel;

namespace InputSide
{
internal class CsvLoader
{
    private int columnCount = 7;    // backing store
    private string filePath;
    private List<DenialRecord> inputList = new List<DenialRecord>();

    public List<DenialRecord> InputList
    {
        set
        {
            this.inputList = InputList;
        }
        get
        {
            return inputList;
        }
    }

    // Default constructor
    internal CsvLoader()
    {
    }

    // Exposed constructor
    public CsvLoader(List<DenialRecord> InputList,  string FilePath, int ColumnCount)
    {
        this.inputList = InputList;
        this.filePath = FilePath;
    }

    // Load csv to List<T>
    public List<DenialRecord> CsvList(int columnCount)
    {
        private int columnCount;

        using (TextFieldParser csvParser = new Microsoft.VisualBasic.FileIO.TextFieldParser(filePath))
        {
            csvParser.TextFieldType = FieldType.Delimited;
            csvParser.SetDelimiters(",");
            csvParser.HasFieldsEnclosedInQuotes = false;
            csvParser.TrimWhiteSpace = true;

            while (!csvParser.EndOfData)
            {

                try
                {
                    string[] row = csvParser.ReadFields();
                    if (row.Length <= 7)
                    {
                        DenialRecord dr = new DenialRecord(row[0], row[1], row[2], row[3], row[4], row[5], row[6]);
                        inputList.Add(dr);
                    }
                }
                catch (Exception e)
                {
                    // do something
                    Console.WriteLine("Error is:  {0}", e.ToString());
                }
            }
            csvParser.Close();
            csvParser.Dispose();
        }
        return inputList;
    }
}
}

EDIT:

Added error list:

Error 1 } expected c:\users\jas189\documents\visual studio 2013\Projects\InputSide\InputSide\CsvLoader.cs 43 10 InputSide
Error 2 Invalid token 'using' in class, struct, or interface member declaration c:\users\jas189\documents\visual studio 2013\Projects\InputSide\InputSide\CsvLoader.cs 46 13 InputSide
Error 3 ; expected c:\users\jas189\documents\visual studio 2013\Projects\InputSide\InputSide\CsvLoader.cs 46 77 InputSide
Error 4 Invalid token '=' in class, struct, or interface member declaration c:\users\jas189\documents\visual studio 2013\Projects\InputSide\InputSide\CsvLoader.cs 48 41 InputSide
Error 5 Invalid token ';' in class, struct, or interface member declaration c:\users\jas189\documents\visual studio 2013\Projects\InputSide\InputSide\CsvLoader.cs 48 62 InputSide
Error 6 Invalid token '(' in class, struct, or interface member declaration c:\users\jas189\documents\visual studio 2013\Projects\InputSide\InputSide\CsvLoader.cs 49 40 InputSide
Error 7 Invalid token '=' in class, struct, or interface member declaration c:\users\jas189\documents\visual studio 2013\Projects\InputSide\InputSide\CsvLoader.cs 50 53 InputSide
Error 8 Invalid token '=' in class, struct, or interface member declaration c:\users\jas189\documents\visual studio 2013\Projects\InputSide\InputSide\CsvLoader.cs 51 42 InputSide
Error 9 Invalid token ')' in class, struct, or interface member declaration c:\users\jas189\documents\visual studio 2013\Projects\InputSide\InputSide\CsvLoader.cs 53 44 InputSide
Error 10 Invalid token 'if' in class, struct, or interface member declaration c:\users\jas189\documents\visual studio 2013\Projects\InputSide\InputSide\CsvLoader.cs 59 25 InputSide
Error 11 Invalid token '<=' in class, struct, or interface member declaration c:\users\jas189\documents\visual studio 2013\Projects\InputSide\InputSide\CsvLoader.cs 59 40 InputSide
Error 12 Invalid token '(' in class, struct, or interface member declaration c:\users\jas189\documents\visual studio 2013\Projects\InputSide\InputSide\CsvLoader.cs 62 42 InputSide
Error 13 Invalid token ')' in class, struct, or interface member declaration c:\users\jas189\documents\visual studio 2013\Projects\InputSide\InputSide\CsvLoader.cs 62 45 InputSide
Error 14 A namespace cannot directly contain members such as fields or methods c:\users\jas189\documents\visual studio 2013\Projects\InputSide\InputSide\CsvLoader.cs 65 21 InputSide
Error 15 Type or namespace definition, or end-of-file expected c:\users\jas189\documents\visual studio 2013\Projects\InputSide\InputSide\CsvLoader.cs 70 17 InputSide
Error 16 A field initializer cannot reference the non-static field, method, or property 'InputSide.CsvLoader.filePath' c:\users\jas189\documents\visual studio 2013\projects\inputside\inputside\csvloader.cs 46 68 InputSide
Error 17 A field initializer cannot reference the non-static field, method, or property 'InputSide.CsvLoader.csvParser' c:\users\jas189\documents\visual studio 2013\projects\inputside\inputside\csvloader.cs 58 40 InputSide Error 18 A field initializer cannot reference the non-static field, method, or property 'InputSide.CsvLoader.row' c:\users\jas189\documents\visual studio 2013\projects\inputside\inputside\csvloader.cs 61 64 InputSide
Error 19 A field initializer cannot reference the non-static field, method, or property 'InputSide.CsvLoader.row' c:\users\jas189\documents\visual studio 2013\projects\inputside\inputside\csvloader.cs 61 72 InputSide
Error 20 A field initializer cannot reference the non-static field, method, or property 'InputSide.CsvLoader.row' c:\users\jas189\documents\visual studio 2013\projects\inputside\inputside\csvloader.cs 61 80 InputSide
Error 21 A field initializer cannot reference the non-static field, method, or property 'InputSide.CsvLoader.row' c:\users\jas189\documents\visual studio 2013\projects\inputside\inputside\csvloader.cs 61 88 InputSide
Error 22 A field initializer cannot reference the non-static field, method, or property 'InputSide.CsvLoader.row' c:\users\jas189\documents\visual studio 2013\projects\inputside\inputside\csvloader.cs 61 96 InputSide
Error 23 A field initializer cannot reference the non-static field, method, or property 'InputSide.CsvLoader.row' c:\users\jas189\documents\visual studio 2013\projects\inputside\inputside\csvloader.cs 61 104 InputSide
Error 24 A field initializer cannot reference the non-static field, method, or property 'InputSide.CsvLoader.row' c:\users\jas189\documents\visual studio 2013\projects\inputside\inputside\csvloader.cs 61 112 InputSide
Error 25 'InputSide.CsvLoader.CsvList(int)': not all code paths return a value c:\users\jas189\documents\visual studio 2013\projects\inputside\inputside\csvloader.cs 42 35 InputSide
Error 26 'InputSide.CsvLoader.csvParser' is a 'field' but is used like a 'type' c:\users\jas189\documents\visual studio 2013\projects\inputside\inputside\csvloader.cs 48 17 InputSide
Error 27 'Microsoft.VisualBasic.FileIO.FieldType.Delimited' is a 'field' but is used like a 'type' c:\users\jas189\documents\visual studio 2013\projects\inputside\inputside\csvloader.cs 48 53 InputSide Error 28 'InputSide.CsvLoader.csvParser' is a 'field' but is used like a 'type' c:\users\jas189\documents\visual studio 2013\projects\inputside\inputside\csvloader.cs 49 17 InputSide
Error 29 'InputSide.CsvLoader.csvParser' is a 'field' but is used like a 'type' c:\users\jas189\documents\visual studio 2013\projects\inputside\inputside\csvloader.cs 50 17 InputSide
Error 30 'InputSide.CsvLoader.csvParser' is a 'field' but is used like a 'type' c:\users\jas189\documents\visual studio 2013\projects\inputside\inputside\csvloader.cs 51 17 InputSide
Error 31 'InputSide.CsvLoader.csvParser' is a 'field' but is used like a 'type' c:\users\jas189\documents\visual studio 2013\projects\inputside\inputside\csvloader.cs 53 25 InputSide
Error 32 'InputSide.CsvLoader.row' is a 'field' but is used like a 'type' c:\users\jas189\documents\visual studio 2013\projects\inputside\inputside\csvloader.cs 59 29 InputSide
Error 33 'InputSide.CsvLoader.inputList' is a 'field' but is used like a 'type' c:\users\jas189\documents\visual studio 2013\projects\inputside\inputside\csvloader.cs 62 29 InputSide
Error 34 'InputSide.CsvLoader.dr' is a 'field' but is used like a 'type' c:\users\jas189\documents\visual studio 2013\projects\inputside\inputside\csvloader.cs 62 43 InputSide

2
Please read How to Ask and provide a minimal reproducible example instead of explaining what your code looks like. - CodeCaster
I can add the class used by the list, but it would be equally irrelevant. What the heck: - Jim Snyder
In what way does it not work? - Andrew Morton
Please be very explicit in what way this fails. What does "It fails to recognize" mean exactly? Do you get a compiler error? What exactly does it say? My best guess is that you actually created this class in a separate class library, and failed to add a reference to Microsoft.VisualBasic in this library... - CodeCaster
Apart from what is probably a missing reference to Microsoft.VisualBasic: in CsvList 1) You can't declare private int columnCount; because that name is used as a parameter and 2) You should use if (row.Length == 7) because if there are less than seven elements then DenialRecord dr = new DenialRecord(row[0], row[1], row[2], row[3], row[4], row[5], row[6]); will fail with an "Index out of bounds" error at run-time. - Andrew Morton

2 Answers

1
votes

Apart from what is possibly a missing reference to Microsoft.VisualBasic:

In CsvList

  • You can't declare private int columnCount; because that name is already used as a parameter and
  • You should use if (row.Length == 7) because if there are less than seven elements then DenialRecord dr = new DenialRecord(row[0], row[1], row[2], row[3], row[4], row[5], row[6]); will fail with an "Index out of bounds" error at run-time.
-1
votes

Final working code:

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualBasic.FileIO;
using ExportToExcel;

namespace InputSide
{
    internal class CsvLoader
    {
        private int columnCount = 7;    // backing store
        private string filePath;
        private List<DenialRecord> inputList = new List<DenialRecord>();

        public List<DenialRecord> InputList
        {
            set
            {
                this.inputList = InputList;
            }
            get
            {
                return inputList;
            }
        }

        // Default constructor
        internal CsvLoader()
        {
        }

        // Exposed constructor
        public CsvLoader(List<DenialRecord> InputList,  string FilePath, int ColumnCount)
        {
            this.inputList = InputList;
            this.filePath = FilePath;
        }

        // Load csv to List<T>
        public List<DenialRecord> CsvList(int columnCount)
        {
            using (TextFieldParser csvParser = new TextFieldParser(filePath))
            {
                csvParser.TextFieldType = FieldType.Delimited;
                csvParser.SetDelimiters(",");
                csvParser.HasFieldsEnclosedInQuotes = false;
                csvParser.TrimWhiteSpace = true;

                while (!csvParser.EndOfData)
                {

                    try
                    {
                        string[] row = csvParser.ReadFields();
                        if (row.Length <= 7)
                        {
                            DenialRecord dr = new DenialRecord(row[0], row[1], row[2], row[3], row[4], row[5], row[6]);
                            inputList.Add(dr);
                        }
                    }
                    catch (Exception e)
                    {
                        // do something
                        Console.WriteLine("Error is:  {0}", e.ToString());
                    }
                }
                csvParser.Close();
                csvParser.Dispose();
            }
            return inputList;
        }
    }
}