Solved

Find character string within line from textfile

Posted on 2016-08-03
3
33 Views
Last Modified: 2016-08-04
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
Comment
Question by:Scarlett72
3 Comments
 
LVL 42

Accepted Solution

by:
zephyr_hex earned 300 total points
ID: 41741165
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
 
LVL 29

Assisted Solution

by:anarki_jimbel
anarki_jimbel earned 200 total points
ID: 41741695
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
 

Author Closing Comment

by:Scarlett72
ID: 41742565
Thank you both zephyr and anarki.  You've helped me understand the concepts a little better.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now