0

I'm new to C# and trying to get my head around why the code below isn't working. I've tried to make a custom class HtmlRequest that isn't static so can be instantiated as many times as required using HtmlRequest someVar = new HtmlRequest();

return sb is holding the value but it's not being returned to hmtmlString on the line htmlString = htmlReq.getHtml(uri).

I've tried putting Get{code ...return sb;} after public class HtmlRequest but can't get the correct syntax

   public partial class MainWindow : DXWindow
    {

            private void GetLinks()
            {
                HtmlRequest htmlReq = new HtmlRequest();
                Uri uri = new Uri("http://stackoverflow.com/");
                StringBuilder htmlString = new StringBuilder();
                htmlString = htmlReq.getHtml(uri); //nothing returned on htmlString

            }

    }

    public class HtmlRequest
    {

        public StringBuilder getHtml(Uri uri)
        {
                // used to build entire input
                StringBuilder sb = new StringBuilder();

                // used on each read operation
                byte[] buf = new byte[8192];

                // prepare the web page we will be asking for
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);

                // execute the request
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                // we will read data via the response stream
                Stream resStream = response.GetResponseStream();

                string tempString = null;
                int count = 0;

                Do
                {
                    // fill the buffer with data
                    count = resStream.Read(buf, 0, buf.Length);

                    // make sure we read some data
                    if (count != 0)
                    {
                        // translate from bytes to ASCII text
                        tempString = Encoding.ASCII.GetString(buf, 0, count);

                        // continue building the string
                        sb.Append(tempString);
                    }
                }
                while (count > 0); // any more data to read?

                return sb;

        }

    }

If I put a breakpoint on return sb; then the variable is correct but is not returning it. It's probably something really obvious, can someone explain why it's not working and how to fix it?

Thank you

3
  • 5
    Try using the value instead of immediately exiting the method. An optimized build won't save the return value if it isn't used. Commented Sep 26, 2012 at 20:11
  • 1
    i just tried this out and it works on this machine..? Commented Sep 26, 2012 at 20:13
  • @AustinSalonen - thanks that's it. I put a breakpoint on a temporary line string pause; and checked. If I actually use the variable then it has a value. That's a bit annoying! Is there any reason for this behaviour? If you add that as an answer I'll accept. Commented Sep 26, 2012 at 20:20

2 Answers 2

1

Try using the value instead of immediately exiting the method. An optimized build won't save the return value if it isn't used.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you - I've now discovered one reason why it's better to start debugging in Debug configuration rather than Release!
1

No need for this:

StringBuilder htmlString = new StringBuilder();
htmlString = htmlReq.getHtml(uri);

It's enough to say:

StringBuilder htmlString = htmlReq.getHtml(uri);

You'd have to define nothing. Nothing means "null", "garbage", what? Is htmlString the object it used to be? Or maybe the function does not return at all? What is it?

1 Comment

Thanks Marius - I did have your suggestion originally but changed it whilst debugging.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.