Scott Van Vliet

Less Talk, More Rock

March 2006 - Posts

Simple Javascript Object Dump Function

In my years of doing Web development, I've written this function several times over; however, I can NEVER find the code when I need it!  Thus, here is a simple Javascript function to dump the contents of any object (posted to share, but also for my own permanent reference!)

 

<script language="javascript">

      

       var MAX_DUMP_DEPTH = 10;

      

       function dumpObj(obj, name, indent, depth) {

              if (depth > MAX_DUMP_DEPTH) {

                     return indent + name + ": <Maximum Depth Reached>\n";

              }

              if (typeof obj == "object") {

                     var child = null;

                     var output = indent + name + "\n";

                     indent += "\t";

                     for (var item in obj)

                     {

                           try {

                                  child = obj[item];

                           } catch (e) {

                                  child = "<Unable to Evaluate>";

                           }

                           if (typeof child == "object") {

                                  output += dumpObj(child, item, indent, depth + 1);

                           } else {

                                  output += indent + item + ": " + child + "\n";

                           }

                     }

                     return output;

              } else {

                     return obj;

              }

       }

      

</script>

 

This will spit out the contents of the object (and nested objects) to an indented string, which is useful when trying to inspect the contents of DOM, etc.  Note that a recursive method like this can be dangerous, so I’ve included a maximum depth flag, MAX_DUMP_DEPTH, to ensure your browser doesn’t get locked up in an infinite nested loop.

 

Enjoy!

Macrodobe MAX 2006 Announced!

Ok, so I took some liberty in the event's name - but via Ben's blog, there WILL in fact be a MAX 2006, and this year it'll be in Las Vegas!  There are no other details, but hopefully they'll announce the call for speakers and I'll get a chance to present again (for my third consecutive year.)

.NET Reference Messenger Bot - Broken Links Fixed

I installed Community Server 2.0 on my server last night, and it removed my downloads folder, thus breaking the links.  I've since re-uploaded the downloads, and the links to the NRB source packages have been fixed.

Just for posterity:

http://www.scottvanvliet.com/downloads/NRB.zip (1.1)
http://www.scottvanvliet.com/downloads/NRB_fx_2_0.zip (2.0, duh!)

Thanks to Danny Boyd for pointing this out!

Posted: Mar 18 2006, 11:26 AM by skillet | with no comments
Filed under:
.NET Reference Messenger Bot - Source Posted for 2.0

The code previously posted for the .NET Reference Messenger Bot was built for .NET 1.1, and when running under 2.0 an InvalidOperationException occurs (thanks to Jeff Gonzalez for pointing this out.)

 

I’ve since modified the code to work in 2.0, and posted a separate download at the following link.

 

http://www.scottvanvliet.com/downloads/NRB_fx_2_0.zip

 

For those of you that are interested in the cause of this error:

 

The DotMSN API implementation responds to interactions with MSN Messenger on different threads.  The test application simply handled those thread callbacks and set the some properties on controls in the form directly.  The problem with this approach is that it is not thread-safe.  In 1.1, this bad practice does not generate an error, nor is it validated for thread safety.  However, 2.0 does a better job (albeit at runtime) of trapping such conflicts and throws the InvalidOperationException.

 

Thus, to fix the problem, the interactions between the callbacks and the form have to be thread safe.  To do this, I added the following code to the form:

 

private delegate void SetTextHandler(object control, string text, bool append);

private void SetText(object control, string text, bool append)

{

    if (this.txtLog.InvokeRequired)

    {

        SetTextHandler d = new SetTextHandler(SetText);

        this.Invoke(d, new object[] { control, text, append });

    }

    else

    {

        System.Reflection.PropertyInfo textProperty = control.GetType().GetProperty("Text");

        text = (append) ? String.Concat(textProperty.GetValue(control, null), text) : text;

        textProperty.SetValue(control, text, null);

    }

 

}

 

This method will allow the form to delegate control manipulation to the main thread under which the form is executing.  Note that using reflection for setting text is not part of this solution – it just made the refactoring effort a little easier as multiple controls in this test application had their Text properties changed by API callbacks.

 

Please let me know if you encounter other errors with this test application, or have any questions :)

Posted: Mar 17 2006, 12:36 PM by skillet | with 2 comment(s)
Filed under:
.NET Reference Messenger Bot - Source Code Posted

After receiving numerous requests for the .NET Reference Bot source, I’ve finally taken the time to dig up the code and post it here:

 

http://www.scottvanvliet.com/downloads/NRB.zip

 

This package includes the DotMSN API (which provides the meat of MSN functionality) and a test application, NRBTest.  To get the test application up and running, you’ll need to modify the App.config file to include a valid MSN Messenger account (underwhich the bot will run.)

 

<?xml version="1.0" encoding="utf-8" ?>

<configuration>

       <appSettings>

              <add key="account" value="youraccount@hotmail.com" />

              <add key="password" value="password" />

       </appSettings>

</configuration>

 

Once you’ve update the App.config file, run the NRBTest application and click the Connect button to start the bot.

 

 

The MS Access database provided with this sample only contains data for the System and System.Data namespaces.

Posted: Mar 16 2006, 09:23 PM by skillet | with 11 comment(s)
Filed under:
WPF Book Update, Going Dark and All that Jazz

It seems that I fall into a pattern of consistent posting over one month, and then go dark over the course of several.  And that’s not just cause I’m lazy (well, maybe a little bit) – but I’ve been busy!

 

I just completed the first chapter for our WPF book and delivered to my DE!  This chapter provides an in-depth overview of the WPF architecture and the relevant subsystems therein.  It was really fun to write, and although time consuming, I’m confident it’s worth the effort.

 

Now that I’ve got this chapter out of the way, I’ll be moving on to another which will provide me the opportunity to post some snippets on this blog for your enjoyment.  If anyone reading this has suggestions for the book, or would like to see a specific example, please post your feedback.  I’d be more than happy to take on the challenge, and even include your ideas in the book!

More Posts