Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7662605
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:50:34+00:00 2026-05-31T13:50:34+00:00

I have the following bits of code, scattered throughout my application. I’d really like

  • 0

I have the following bits of code, scattered throughout my application. I’d really like to boilerplate it, and place it in either a static class, or some utility set of classes so I don’t have all this duplication.

However, the small bits of the function are unique in such a way that I don’t know how to refactor it.

private void callResponseCallback(IAsyncResult asynchronousResult)
        {
            try
            {
                HttpWebRequest webRequest = (HttpWebRequest)asynchronousResult.AsyncState;
                HttpWebResponse response;

                // End the get response operation
                response = (HttpWebResponse)webRequest.EndGetResponse(asynchronousResult);
                Stream streamResponse = response.GetResponseStream();
                StreamReader streamReader = new StreamReader(streamResponse);
                string responseData = streamReader.ReadToEnd();
                streamResponse.Close();
                streamReader.Close();
                response.Close();


                ExpectedResponseType regResponse = Newtonsoft.Json.JsonConvert.DeserializeObject<ExpectedResponseType>(responseData);

                if (regResponse.ok == "0")
                {
                    //error - handle the msg
                    //whether the user not loggin or not exist 
                    Deployment.Current.Dispatcher.BeginInvoke(() =>
                    {

                         MessageBox.Show(CustomErrorMessage);

                    });

                }
                else
                {
                    //check the variables unique to the ExpectedResponseType and do Stuff here;                      


                }
            }
            catch (WebException e)
            {
                // Error treatment
                // ...

                Debug.WriteLine("error " + e);
            }

I am most curious how to pass in “ExpectedResponseType”, such that it might be any Class, (i.e., is there a way to pass in T?) or possibly how to fire events that can then be executed by the UI thread and handled appropriately.

Thanks.

edit: “ExpectedResponseType” or “T” is a large collection of classes for each type of server call. For example I have LoginResponse, RegisterResponse, GetFilesResponse, UpdateResponse, DownloadResponse, etc..

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T13:50:36+00:00Added an answer on May 31, 2026 at 1:50 pm

    EDIT: I have removed earlier example as it would not work with the delegate signature.

    In order to handle the checking of the parameters specific to the type T you will need to add a little abstraction, the cleanest way is probably to wrap your code in a templated class that allows the registration of a delegate for handling the checking, I’m sure this is a specific pattern but cannot recall which one:

    public class ResponseHandler<T>
    {
        public ResponseHandler(Action<T> typeSpecificCheckFunction)
        {
            this.CheckVariables = typeSpecificCheckFunction;
        }
        Action<T> CheckVariables;
        public void callResponseCallback(IAsyncResult asynchronousResult)
        {
            // stuff
            T regResponse = Newtonsoft.Json.JsonConvert.DeserializeObject<T>(responseData);
            CheckVariables(response);
            // stuff
        }
    } 
    

    In response to you question about handling a large variety of T, perhaps the cleaned up code above clears it up, if not then this is what generics are for – provided you know what you are expecting in each case. So for each type you were expecting you would call it something along the lines of:

    var handler = new ResponseHandler<ExpectedResponseType>( response => 
                            {
                                 // code to check your response properties here 
                            });
    
    xxx.RegisterResponseCallback(handler.callResponseCallback);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following bits of code which, in my mind, seem like the
I have the following code fragment: template <class T> struct ServicePtr { std::shared_ptr<T> service;
I have the following bits of code: <h:panelGroup rendered=#{userTypeController.permissionItemsUserType.contains(item)}> <h:selectBooleanCheckbox valueChangeListener=#{userTypeController.permissionChanged(this)} value=#{true}/> <h:outputText value=#{item.getAction()}
I have the following bits of code in my accountRepository public string[] GetRolesForUser(string email)
I have the following code that does a circular shift of the bits in
I have the following code which tries to add a class of selected to
In our code I have the following, for now please ignore the //* bits;
I Have following code: Controller: public ActionResult Step1() { return View(); } [AcceptVerbs(HttpVerbs.Post)] public
I have following Code Block Which I tried to optimize in the Optimized section
I have following classes. class A { public: void fun(); } class B: public

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.