Solved

Find character string within line from textfile

Posted on 2016-08-03
3
38 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 43

Accepted Solution

by:
zephyr_hex (Megan) 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 30

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…

705 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