C# C# Streams and Data Processing Streaming Data on the Net WebClient

Aaron Selonke
Aaron Selonke
10,323 Points

Passing the WebClient Challange

1) In task Three Directions say "In a using statement, create a MemoryStream object named stream, and pass the treehouseBytes variable as a parameter. Add another using statement around a new StreamReader object named reader. Pass the stream into the StreamReader constructor."

 using(var webClient = new WebClient())
            {
            byte[] treehouseBytes = webClient.DownloadData("https://www.teamtreehouse.com");
            }

If I wrap the new MemoryStream object in a new Using statement how can I pass the treehouseBytes in as a parameter. It does not exist before or after the webclient's using statement. How does this go?

2) Task One directions remind us that "The WebClient class implements IDisposable, so wrap it in a using statement." ...Why were we not wrapping the WebClient object in a using statement when learning how to use it in the video??

2 Answers

Steven Parker
Steven Parker
182,341 Points
  1. The variable treehouseByte exists within the first using statement, and the new using statement will be placed after treehouseBytes is created but still within the original using.

  2. I wondered about that one myself. Perhaps a video boo-boo?

Aaron Selonke
Aaron Selonke
10,323 Points

Yeah, right before you replied I was starting to think that you have to nest the using statements

using (var webClient = new WebClient())
            {
                webClient.Headers.Add("Ocp-Apim-Subscription-Key", "5739a899674c4b92955879175f5c3c26");
                byte[] ByteData = webClient.DownloadData(string.Format("https://api.cognitive.microsoft.com/bing/v5.0/news/search?q={0}&mkt=en-us", topic[0]));

                using (var memStream = new MemoryStream(ByteData))
                using (var reader = new StreamReader(memStream))
                using (var jreader = new JsonTextReader(reader))
                {
                    var cereal = new JsonSerializer();
                    return cereal.Deserialize<NEWS>(jreader).NewsResults;
                }
            }

I found this is a better way to organize the code from the project, because its easier to remember the syntax and logic of all the pieces working together

Michael Kroon
Michael Kroon
16,253 Points

@StevenParker:: Yeah, it took some monkeying around but I was finally able to complete this challenge with the help of your comments. Even though nothing is being done with the 3rd 'using' block, yet, I still needed the brackets to actually pass the challenge. Here's my solution:

using System;
using System.IO;
using System.Net;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(GetTreehouseHome());
        }

        public static string GetTreehouseHome()
        {
            string treehouse = "";
            // Put code here
            using(var webClient = new WebClient())
            {
                byte[] treehouseBytes = webClient.DownloadData("https://www.teamtreehouse.com");
                using(var stream = new MemoryStream(treehouseBytes))
                {
                    using(var reader = new StreamReader(stream))
                    {
                    }
                }

            }

            return treehouse;
        }
    }
}
Ethan Batten
Ethan Batten
3,229 Points

You have the to nest the using statements. Check out my example below. I also believe its a mess up in the wording.

using System; using System.IO; using System.Net;

namespace Treehouse.CodeChallenges { class Program { static void Main(string[] args) { Console.WriteLine(GetTreehouseHome()); }

    public static string GetTreehouseHome()
    {
        string treehouse = "";
        // Put code here
        using(WebClient webClient = new WebClient())
        {
            byte[] treehouseBytes = webClient.DownloadData("https://www.teamtreehouse.com");
            using(var stream = new MemoryStream(treehouseBytes))
            {
                using(var reader = new StreamReader(stream));
            }
        }





        return treehouse;
    }
}

}