Copying and Pasting cs Code

In cs, like in almost any computer programming language, reading data from a file can be tricky. You add extra lines of code to tell the computer what to do. Sometimes you can copy and paste these lines from other peoples’ code.

For example, you can follow the pattern in this listing:

     private void OnRegisterByEmailCompleted(IAsyncResult ar)
     {
         try
         {
             HttpWebRequest request = (HttpWebRequest)ar.AsyncState;
             HttpWebResponse response = request.EndGetResponse(ar) as HttpWebResponse;

             if (response != null && response.StatusCode == HttpStatusCode.OK)
             {
                 // no error. use the result
                 StreamReader reader = new StreamReader(response.GetResponseStream());
                 string result = reader.ReadToEnd();

                 this.ParseResult(result);
             }
             else
             {
                 // a response but some error on server. show message
                 this.Message = "Failed to connect to Cloud Account Service. Please register via account website.";
             }
         }
         catch (Exception ex)
         {
             // not even a response. show message
             this.Message = "Failed to connect to Cloud Account Service. Please register via account website.";
             this.Exception = ex;
         }

         if (this.registrationCallback != null)
         {
             this.registrationCallback(this);
         }
     }