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'

4
  • 1
    What part of this doesn't work? If you want the cumulative count, then you'll have to sum the individual lengths. Commented Jun 26, 2015 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'" Commented Jun 26, 2015 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. Then you get an int that you want to assign to a string. For that see Convert int to string? or just put the compiler error you get in your favorite web search engine. Commented Jun 26, 2015 at 15:23
  • @CodeCaster Do I need to convert Data_array into an int before applying Count()? Commented Jun 26, 2015 at 15:26

1 Answer 1

6

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

Sign up to request clarification or add additional context in comments.

3 Comments

That still gives me the error: Cannot convert Method group 'ToString' to non-delegate type 'string'.....
@manateejoe then you again think you can omit the parentheses from a method call. You can't, this isn't Visual Basic.
@CodeCaster I have added the parentheses and I am still getting the error EDIT: Never mind, restarting the program helped. Thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.