Sep
20
2010

Interstellar Pong v4.0

splashScreenI am “working” this week at Interface Technical Training by attending the new Silverlight 4.0 class taught by Dan Wahlin.

While Dan was dazzling the attendees with his Silverlight prowess, I got inspired to convert a game I wrote in version 1.0 of Silverlight to the latest version.

While the game is upgraded to v4.0 of Silverlight, the game could use more revisions in terms of feature enhancements.  Nonetheless, I offer the humble code to anyone who would like to make improvements.

    Copyright © Microsoft Corporation. All rights reserved.
    The code provided in this post is licensed under the Microsoft Public License (Ms-PL).
Jul
26
2010

How to Get jQuery 1.4.2 IntelliSense to Work in Visual Studio 2008

jQueryIsReady For those still using Visual Studio 2008 and developing using jQuery, you will no doubt desire the use of IntelliSense in your text editor to browse the jQuery objects.

To make sure you get this benefit, you must have SP1 for Visual Studio 2008 applied and run a patch which you can download here.

Download jQuery 1.4.2 and the –vsdoc.js for jQuery 1.4.1 and place them in the same folder in your web app.  Rename the –vsdoc.js file to “jquery-1.4.2.min-vsdoc.js”. 

Add a script tag to the top of your ASP.NET web form or master page:

 

You should now be able to see IntelliSense at work anywhere you reference jQuery.

    Copyright © Microsoft Corporation. All rights reserved.
    The code provided in this post is licensed under the Microsoft Public License (Ms-PL).
Jul
23
2010

Getting the Event-Causing Control ID in ASP.NET Web Forms

Very early in the life of a web form in ASP.NET, you may need to know what control fired the event that caused the post-back to occur.  Perhaps you need to know what button was clicked before the click event handler is executed.  If that scenario is the one you find yourself in, then the following code can help!

If you include the following extension method in your code, you can get the ID of the event causing control from the HttpRequest instance:

public static string GetEventTargetId(this HttpRequest req)
{
    return GetOriginalControlId(req.Form["__EVENTTARGET"]);
}// extension method

The above code is dependent on a helper method to strip the ugly nonsense potentially added while the control was rendered (can now be avoided in ASP.NET 4.0).  The helper method is defined below:

public static string GetOriginalControlId(string renderedControlId)
{
    if (renderedControlId == null) return null;
    int indexOfSeparator = renderedControlId.LastIndexOf('$');
    if (indexOfSeparator >= 0)
    {
        renderedControlId = renderedControlId.Substring(indexOfSeparator + 1);
    }
    return renderedControlId;
}// method
    Copyright © Microsoft Corporation. All rights reserved.
    The code provided in this post is licensed under the Microsoft Public License (Ms-PL).
Jul
9
2010

Is Your jQuery Ready?

For those that don’t know this, you can shorthand the following script:

$(document).ready(function () {
    alert("Giddyup!");
});

… and replace the above with the following equivalent:

$(function () {
    alert("Giddyup!");
});
    Copyright © Microsoft Corporation. All rights reserved.
    The code provided in this post is licensed under the Microsoft Public License (Ms-PL).
Jul
8
2010

How to Get the Last Number of an Integer

When I teach my introduction to C# course, I typically provide a tough numeric challenge to stimulate thinking and use all the features of the language learned up to that point.  In a recent class, I was presented the challenge of determining the last number of any integer.  For example:

  • 12 == 2
  • 337 == 7
  • 1000 == 0
  • 987654 == 4

In each example above, I needed to simply return whatever was in the “singles” place – the last number of the number.

My first approach felt like I was cheating, but it worked:

public static int GetLastNumber(string stringifiedNumber)
{
    return Convert.ToInt32(stringifiedNumber[stringifiedNumber.Length -1].ToString());
}// method

After brooding over it, I decided to challenge myself to do this mathematically instead.  Here is the final code:

public static int GetLastNumber(dynamic anyNumber)
{
    double fractional = anyNumber * .1;
    double truncated = Math.Truncate(fractional);
    return (int)(Math.Round((fractional - truncated),1) * 10);
}// method

The use of the dynamic keyword above allows me to pass in any numeric data type.

Updated: Thanks to Nicki for identifying how I over-engineered this problem.  I simply needed to get the modulus of 10 to get what I wanted.  So in one simple statement I could write:

 anyNumber % 10;
    Copyright © Microsoft Corporation. All rights reserved.
    The code provided in this post is licensed under the Microsoft Public License (Ms-PL).

Resources

Archives

Team Blogs

Download OPML file OPML