Search code examples
.netaxaptadynamics-ax-2012

Getting array from .Net parm method


I am trying to use the .Net class Microsoft.VisualBasic.FileIO.TextFieldParser to read in a csv. The problem I am having is that the value of the variable netArray appears to be being set to a single string, without the values being split to separate array entries. Any idea as to why this would happen?

int counter, xppArrayLength;
str xppValue;
str arr[];
System.String[]     netArray;
System.String[]     delimeters = new System.String[1]();
Microsoft.VisualBasic.FileIO.TextFieldParser parser = new Microsoft.VisualBasic.FileIO.TextFieldParser(myPath);

delimeters.SetValue(",",0);
parser.set_Delimiters(delimeters);
parser.set_HasFieldsEnclosedInQuotes(true);

netArray = parser.ReadLine();

while(netArray.get_Length())
{
    xppArrayLength = netArray.get_Length();
    for(counter = 1; counter <= xppArrayLength; counter++)
    {
        xppValue = netArray.GetValue(counter-1);
        arr[counter] = xppValue;

    }

    netArray = parser.ReadLine();

} }


Solution

  • Because TextFieldParser.ReadLine() is supposed to return a single string (as documented here https://msdn.microsoft.com/en-us/library/6a8fts4w%28v=vs.90%29.aspx)

    You should be doing parser.ReadFields() (https://msdn.microsoft.com/en-us/library/microsoft.visualbasic.fileio.textfieldparser.readfields%28v=vs.110%29.aspx)

    Also you've not set TextFieldType = FieldType.Delimited (as documented here https://msdn.microsoft.com/en-us/library/63k8k5sx%28v=vs.90%29.aspx) TextFieldType default value is FieldType.Delimited, so no need to set it.