none
Lesson 18-Using Visual Studio 2017 I get error CS0161 'Scrape.GetWebpage(string)': not all code paths return a value. RRS feed

  • Question

  • Screenshot follows

    Course: C# Fundamentals for Absolute Beginners Lesson 18

    Course url: https://mva.microsoft.com/en-US/training-courses/c-fundamentals-for-absolute-beginners-16169?l=XIMI21QIC_2406218949

    I wasn't allowed to include the jpg of the Visual Studio screen. The code follows and the wording of the error is in the title of the question but I repeat it here: Error CS0161 'Scrape.GetWebpage(string)': not all code paths return a value. I underlined the offending private method at bottom of code..

    Thanks

    I believe my code follows the example but it has an error that does not occur in the video.

    My code:

     

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Net;
    using System.Text;
    using System.Threading.Tasks;

    namespace MyCodeLibrary
    {
        public class Scrape
        {
            public string ScrapeWebpage(string url)
            {
                return GetWebpage(url);
            }

            public string ScrapeWebpage(string url, string filepath)
            {

                string reply = GetWebpage(url);

                File.WriteAllText(filepath, reply);
                return reply;
            }

            private string GetWebpage(string url)
            {
                WebClient client = new WebClient();
                string reply = client.DownloadString(url);
            }
        }
    }

    This is a pretty fundamental chunk in the learning chain: referencing asemblies. I'd like to get it right.

    Thanks,

    Rex Brooks

    Thursday, April 13, 2017 2:09 PM

All replies

  • well the error means you need to add a return statement like all other methods.


    Visual C++ MVP

    Thursday, April 13, 2017 5:36 PM
  • You are saying that the method will return a string back to caller of it, but you are returning nothing, so your method:

     private string GetWebpage(string url)
     {
          WebClient client = new WebClient();
          string reply = client.DownloadString(url);
     }

    needs to be :

     private string GetWebpage(string url)
     {
          WebClient client = new WebClient();
          string reply = client.DownloadString(url);
     
          return reply;
     }

    Hope it Helps!


    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Blog | LinkedIn | Stack Overflow | Facebook
    profile for Ehsan Sajjad on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, April 13, 2017 6:45 PM
  • As mentioned the GetWebpage must return a string since you have defined its return type as string.

    You should also always remember to dispose all objects that implements the IDiposable interface as soon as you are done using them. The easiest way to do this is to use a using statement. So your GetWebpage method should be implemented like this:

                    private string GetWebpage(string url)
                     {
                         using (WebClient client = new WebClient())
                             return client.DownloadString(url);
                     }

    Hope that helps.

    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.


    Thursday, April 13, 2017 7:01 PM