3

I am reading in data from a file into a list. Once I have the data read in, I would like to split every string, and display the length of the split string. I have tried string.Length and string.Count, but nothing works. How can I find the number of elements in my split string?

Here is the section of relevant code:

string fileread = openFileDialog1.Filename; //Opens file from computer
lines = System.IO.File.ReadAllLines(fileread);
foreach (string line in lines)
{
    string[] Data_array = line.Split(',');
    List<string> Data_list = new List<string>();
    Data_list.Add(line);
    lbl_datacolumns.Text = Data_array.Count;//should show number of elements in Data_array
}

The error is: "Cannot Convert method group 'Count; to non-delegate type 'string'

manateejoe
  • 344
  • 2
  • 6
  • 19
  • 1
    What part of this doesn't work? If you want the cumulative count, then you'll have to sum the individual lengths. – Sami Kuhmonen Jun 26 '15 at 15:19
  • @DStanley That's alright, I only need the last one, all lines should be equal length. The problem is: "Cannot Convert method group 'Count; to non-delegate type 'string'" – manateejoe Jun 26 '15 at 15:23
  • 1
    Try to search. `Count()` is a (extension) method, so you need to add parentheses to make it a method call. See [Cannot convert method group 'ToList' to non-delegate type](http://stackoverflow.com/questions/7730302/cannot-convert-method-group-tolist-to-non-delegate-type). Then you get an `int` that you want to assign to a `string`. For that see [Convert int to string?](http://stackoverflow.com/questions/3081916/convert-int-to-string) or just put the compiler error you get in your favorite web search engine. – CodeCaster Jun 26 '15 at 15:23
  • @CodeCaster Do I need to convert Data_array into an int before applying Count()? – manateejoe Jun 26 '15 at 15:26

1 Answers1

5

Use the Length property:

lbl_datacolumns.Text = Data_array.Length.ToString();

https://msdn.microsoft.com/en-us/library/system.array(v=vs.110).aspx

dotnetfiddle: https://dotnetfiddle.net/5m6Wk8

ps2goat
  • 8,067
  • 1
  • 35
  • 68
  • That still gives me the error: Cannot convert Method group 'ToString' to non-delegate type 'string'..... – manateejoe Jun 26 '15 at 15:22
  • @manateejoe then you again think you can omit the parentheses from a method call. You can't, this isn't Visual Basic. – CodeCaster Jun 26 '15 at 15:24
  • @CodeCaster I have added the parentheses and I am still getting the error EDIT: Never mind, restarting the program helped. Thanks! – manateejoe Jun 26 '15 at 15:27