• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 44
  • Last Modified:

Find character string within line from textfile

Using a windows application form, I am trying to read a text file and find if a string pattern he text file.
I am receiving the error:  Cannot implicitly convert type 'System.Threading.Tasks.Task<string>' to 'string[]'


    public partial class Form1 : Form
    {
        String line;
        StringBuilder sb = new StringBuilder();
        String[] lines;

        public Form1()
        {
            InitializeComponent();
        }

        private async void button1_Click(object sender, EventArgs e)
        {          
            try
            {
                using (StreamReader sr = 
                    new StreamReader(@"\\test.txt"))
                {
                    lines = sr.ReadToEndAsync(); //line where error points to ...
                    foreach(String line in lines)
                    {
                        if (line.Contains("902"))
                        {
                            sb.Append(line);
                        }
                    }                    
                    textBox1.Text = sb.ToString();
                }
            }
            catch (Exception)
            {
                textBox1.Text = "Could not read the file";
                //throw;
            }
        }
    }

Open in new window

0
Scarlett72
Asked:
Scarlett72
2 Solutions
 
zephyr_hex (Megan)DeveloperCommented:
sr.ReadToEndAsync() is returning something of type Task<string>
lines is defined as String[]

Try defining lines as
var lines = new List<Task>();

Open in new window


And then add the task to the list:
lines.Add(sr.ReadToEndAsync());

Open in new window


So your code would look like:
 public partial class Form1 : Form
    {
        String line;
        StringBuilder sb = new StringBuilder();
        var lines = new List<Task>();
        //... other stuff

        {
                    lines.Add(sr.ReadToEndAsync()); //line where error points to ...
        //... other stuff

Open in new window

1
 
anarki_jimbelCommented:
Really Zephyr_hex explained what the error was but I'd like to clarify.
The problem is that ReadToEndAsync method returns a Task containing a string.
And as your error says you cannot assign this Task to a string array.
However, according to MS you may assign it to a string, something like below (see https://msdn.microsoft.com/en-us/library/dd321468(v=vs.100).aspx):

string s="";
if(task1.IsReady())
s = task1.Result;

Open in new window


When you have a string representing your file ('s' above) content you may split it to an array or whatever by new line delimiters, if you need, of course.

Also, do you really need asynchronous read?
Just in case, check https://msdn.microsoft.com/en-us/library/system.io.streamreader.readtoendasync(v=vs.110).aspx and
https://msdn.microsoft.com/en-us/library/system.io.streamreader.readtoend(v=vs.110).aspx
1
 
Scarlett72Author Commented:
Thank you both zephyr and anarki.  You've helped me understand the concepts a little better.
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now