Boxing

Sun, July 31, 2005, 04:06 PM under dotNET
If I am interviewing you for a dotnet position and I ask you about boxing I expect you to be able to quickly describe what it is, why one should care and give me a quick example. If I see you struggle, I'll help you by asking "If I add an integer to an ArrayList, does boxing occur?". If you hesitate answering that then... [this really happened recently when I interviewed someone for a senior .NET position].

So it was with pleasure that I saw a post on boxing this week. If you are struggling with the concept go read Raymond Lewallen's Boxing and Unboxing for Beginners.

I don't really have anything to add to that post other than a couple of comments on the Intermediate Language:
1. Note the IL instruction box. That is where boxing gets its name (should be obvious but thought I'd spell it out :-)
2. Raymond's example uses VB and unfortunately has Option Strict Off. If it had Option Strict On, you'd see that when you retrieve the result you have to cast it to the integer
i.e. b = DirectCast(a(0), Int32)

If you make that modification, then you see the other magic IL instruction: unbox (rather than the *really* ugly VB-compiler-injected-statement:
[Microsoft.VisualBasic]Microsoft.VisualBasic.CompilerServices.IntegerType::FromObject(object)).

Here is the IL with that small modification for VB in VS.NET 2003 (note that it is compiled in Release mode so we get cleaner IL):
.method public instance void  Method1() cil managed
{
// Code size 39 (0x27)
.maxstack 2
.locals init (class [mscorlib]System.Collections.ArrayList V_0,
int32 V_1)
IL_0000: newobj instance void [mscorlib]System.Collections.ArrayList::.ctor()
IL_0005: stloc.0
IL_0006: ldloc.0
IL_0007: ldc.i4.s 10
IL_0009: box [mscorlib]System.Int32
IL_000e: callvirt instance int32 [mscorlib]System.Collections.ArrayList::Add(object)
IL_0013: pop
IL_0014: ldloc.0
IL_0015: ldc.i4.0
IL_0016: callvirt instance object [mscorlib]System.Collections.ArrayList::get_Item(int32)
IL_001b: unbox [mscorlib]System.Int32
IL_0020: ldobj [mscorlib]System.Int32
IL_0025: stloc.1
IL_0026: ret
} // end of method Form1::Method1
3. Just so you can see the IL for a C# version compiled under Visual Studio 2005 Beta 2, here is some code:
private ArrayList ar = new ArrayList();
public void Box() {
ar.Add(5);
}
public void Unbox() {
int i = (int)ar[0];
}
...and equivalent IL:
.method public hidebysig instance void  Box() cil managed
{
// Code size 19 (0x13)
.maxstack 8
IL_0000: ldarg.0
IL_0001: ldfld class [mscorlib]System.Collections.ArrayList BoxUnbox.Form1::ar
IL_0006: ldc.i4.5
IL_0007: box [mscorlib]System.Int32
IL_000c: callvirt instance int32 [mscorlib]System.Collections.ArrayList::Add(object)
IL_0011: pop
IL_0012: ret
} // end of method Form1::Box

.method public hidebysig instance void Unbox() cil managed
{
// Code size 19 (0x13)
.maxstack 8
IL_0000: ldarg.0
IL_0001: ldfld class [mscorlib]System.Collections.ArrayList BoxUnbox.Form1::ar
IL_0006: ldc.i4.0
IL_0007: callvirt instance object [mscorlib]System.Collections.ArrayList::get_Item(int32)
IL_000c: unbox.any [mscorlib]System.Int32
IL_0011: pop
IL_0012: ret
} // end of method Form1::Unbox
By the way, a great follow up to the above is to show how Generics eliminate the need for boxing in such a scenario pointing out that
a) No boxing/unboxing penalty takes place
b) No cast is required
c) Compiler prevents anyone adding objects of other types to our List

Blog link of the week 30

Sun, July 31, 2005, 02:00 PM under Links
"Thorough" doesn't begin to describe the coverage: Mouse Input and drag'n'drop.

Highlights of the performance enhancements in Whidbey

Generics Variance

Replies to some JIT questions

Notes and screenshots of longhorn client Windows Vista Beta 1

Visual Form Inheritance with netcf

Thu, July 28, 2005, 12:54 PM under MobileAndEmbedded
One of the features that devs got excited with when Visual Studio .NET 2002 launched, is Visual Form Inheritance. I have never used this feature in a commercial app but I guess I can see its appeal.

.NET Compact Framework v1.0 projects do not support Visual Form Inheritance in the designer (you cannot view the results of inheriting your form from another of your own forms). Specifically, if you attempt to directly inherit from anything other than System.Windows.Form, the winforms designer will not render your form's design surface. However, your application will still compile and execute with the visual inheritance correctly observed at run time.

With Visual Studio .NET 2003, you should do your design work before changing the parent of your form to be another form (i.e. do it while it still inherits from Form and the designer works). When you need the designer again, change your class to inherit from System.Windows.Forms.Form
public class Form4 
//: System.Windows.Forms.Form // Use this line for design time
: Form2 // Use this line for runtime

This has been discussed multiple time previously in the newsgroup archives e.g. here

So what is the story with v2.0 of the compact framework and Visual Studio 2005?

Start with a new Device Application with a single form and add some controls to it. If you navigate to "Project->Add New Item", the dialog does not include the "Inherited Form" template (in Beta 2). However, you can add a "Windows Form" and then change the code to achieve visual form inheritance. Here are the steps:

1. Create a new Smart Device Project
2. To the default Form1, add a button
3. Add a new form (Form2)
4. Delete the mainmenu from Form2
5. Go to the code and change it (Form2.cs) so it does not inherit from Form and instead inherits from Form1
6. Rebuild the solution
7. Open Form2 in the designer and observe how it shows button1

If you are using VB, make sure at step 5 you go to the Form2.Designer.vb (instead of the Form2.vb) - see my previous post on partial classes and how to get to the designer file.

Hopefully by RTM the VSD team will add the template so it will be even easier!

UPDATE: September 2005 - Visual Studio 2005 Release Candidate supports the "Inherited Form" template

Bye bye Lotus Notes

Wed, July 27, 2005, 03:38 PM under UserInterfaceDesign
If you haven't used it then I envy you. If you have, then you know as well as I do that Lotus Notes sucks. It deserved the dedicated pages on the Interface Hall of Shame (cannot find the URL right now).

It is easy for me to write a book on the topic but instead here are just a couple of screens I've personally faced that show the quality of the product (since the non user friendliness of it is well documented).

Over the past 6 years I've had this crappy waste of space app installed on two PCs here at home, a tower at work, an older laptop and my current laptop.

Finally, I have a computer in my possession (the tablet I mentioned earlier this month), which I am setting up and the Notes piece of sh1t will not go on it. Combined with the fact that I am changing jobs, this means that I am not going to meet with that abomination any time soon. Anyway, there may be kids reading so, I am going to hold off telling you how I *really* feel (or start talking about Planta PPMS)!

Blog link of the week 29

Sun, July 24, 2005, 02:26 PM under Links
# Code Snippets FAQ.

# Generic delegates in the framework.

# String Basics (and a few people come to mind that should go back to basics ;-)

# Katie Blanch (previously known as K. Schaeffer) contributes 3 interop articles on the netcf blog:
Extending GUI Functionality
Common Issues and Debugging Techniques
Log File Information

# One of the hot WM 5.0 topics is persistent storage.

# Hah! Recently I had an argument discussion on this topic so I hope my "opponent" reads this: Singleton or ExpensiveResourceSingleton.

WindowsMobile.Forms

Thu, July 21, 2005, 12:08 PM under MobileAndEmbedded
In my previous post I linked to a class diagram that shows all the WM 5.0 managed namespaces. It turns out that diagram doesn't show the WindowsMobile.Forms namespace so here it is for your enjoyment:


Had I drawn it in a C# project all those 'As' would be ':' conforming to UML style but I'll refrain from going on about that again.

Microsoft.Windows.Mobile

Wed, July 20, 2005, 03:20 PM under MobileAndEmbedded
All of you know that Windows Mobile 5.0 was released in May at MEDC (Bill Gates keynote). Devices are expected to ship in Q4 this year.

From a general dotnet perspective, the most exciting thing about this platform release is that it exposes managed APIs! This is a first in both the mobile/embedded and desktop/server world. Maybe Longhorn will be the next, but for now WM 5.0 is the first and only one to do that.

What prompted me to share this is that already there are articles etc that refer to these APIs as being part of .NET Compact Framework v2.0 - they are not, they are part of the platfrom (which also includes CF 1.0 SP3 by the way). If you use netcf 2.0 to target a WM2003 or a WinCE 5.0 device, the APIs are not available. In contrast, if you use netcf v1.0 to target a WM 5.0 device then the APIs are available.

There is a lot of redundancy in my statements above but hopefully it will clarify the state of play (my fear is that this will become another common misconception similar to people talking about v1.1 of compact framework - when there is only v1.0 and v2.0).

The APIs I refer to are classes in the following namespaces:
Microsoft.WindowsMobile.PocketOutlook
Microsoft.WindowsMobile.Configuration
Microsoft.WindowsMobile.Status
Microsoft.WindowsMobile.Telephony
Microsoft.WindowsMobile.Forms

You can access them from Visual Studio 2005 once you download the WM 5.0 SDK and after you add reference to the relevant assemblies (same as namespace names with "dll" suffix)

Just to confuse things, there is a netcf 2.0 namespace with similar name: Microsoft.WindowsMobile.DirectX. If you are into game programming, check it out and also the article here

So, before I let you go back to your work, here is a link if you wish to find out more: recorded webcast & class diagram (the one missing is the Forms namespace which you can see here)

Oh, I almost forgot, if you like the new classes and would like to see them on earlier PPC versions (e.g. PPC 2003), check this out :-)

Blog link of the week 28

Sun, July 17, 2005, 03:50 PM under Links
After delays upon delays I finally just made it back home from Dublin. Some photos from my talk are here (btw, if anybody wants me to talk at a user group or anywhere else, my new term of contract is that it must be followed by a “mouths-on” wine tasting lecture ;-).

So, since I haven’t personally caught up with any blogs this week, BLOTW redirects you to the Mobile Minute 105, 104 and 103.

Free Wine and Cheese Friday 15th

Wed, July 13, 2005, 11:07 AM under Events
If you are in Ireland, don't forget to come join us in Dublin this Friday for a unique wine & cheese tasting experience that follows my talk on Whidbey for devices (with an intermediate stop at the bar for some beer :-).

Register now! (We are flying out there tomorrow this time...)

Blog link of the week 27

Sun, July 10, 2005, 02:57 PM under Links
This time round BLOTW is dedicated to Tech Ed Europe which was on last week. I was there in 2001-2002-2004, but sadly not this year.

Many blogs covered the event and some that caught my eye are here, here, here, here and here.