Solved

Find character string within line from textfile

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to read json value 2 48
System.Security 2 27
In SQL Server 2016, can we configure MASKing rules that applies for application level users ? 12 55
2 questions 10 25
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

776 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