Solved

Find character string within line from textfile

Posted on 2016-08-03
3
37 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

738 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