Smart phones are one of the most intimate devices we own. We take them with us so many places throughout the day, be it in a pocket, bag or purse. We hold them so many times during the day. And at night we often take them to bed with us, usually leaving them on the bedside table. These are computers that have become companion devices, smart devices. Yet in a world of wireless technologies, devices of old still require to be plugged in regularly. Rather than adopt new wireless charging integration, the iPhone 5 designers came up with a new plug design, meaning you’ll have to continually watch your battery life and remember to plug it in: in essence you’ll have to continue to change it’s nappies. It’s infantile.

If you look at the intimacy we have with our smart phones, you’ll probably notice we are often slaves to battery life. Yes it really is like toilet training the phone, trying to pre-empt when it’ll need its next visit to the outlet. Wireless charging changes that. When you put your phone down by your bed it’s charging. Put the phone in your car’s console: it’s charging. Put the phone on your desk or on the table at a coffee shop or airport lounge: it’s charging. No cables, no plugs, no adapters, no having to constantly monitor and remember when to change the nappy.

iPhone 5 doesn’t have this. It is an epic fail. The new “lightning” plug adds to the epic fail.

with no comments
Filed under: , ,

Last week Neil Armstrong passed away. He was part of an era in which innovation truly thrived. Profoundly back in 1969 he said “That’s one small step for man, one giant leap for mankind” . The space race drove innovation and invention. There were so many spin-offs, especially in technology.

Fast forward more than forty years later, we have a jury of nine, ruling pinch to zoom, and icons in a grid are the invention of Apple. Sad.

What a sad sad week  for mankind.

with 2 comment(s)
Filed under: ,

Recently Nokia rolled out a couple of updates for Lumia owners including an OS update that provides tethering support for Lumia 800’s, and some cool camera extras that provides a great group photo merge.  Nokia also released a “Contacts Share” application that provides vcard support for sharing contacts.

Install Nokia’s Contact Share, and next time someone sends you a vcard attachment you can simply open it and select to add it to your contacts. There’s other contacting sharing apps out there, but this is the first one I’ve seen that adds vcard attachment support. The app doesn’t integrate completely into the People hub, but it’s the next best thing.

You might have trouble locating the app in the windows phone market place: it was released in June. Here’s the direct link:

http://windowsphone.com/s?appid=feb55f4c-1e2d-497e-b1ee-fce04a9a11ae

 

Enjoy Smile

with no comments
Filed under: ,

In the Office 2013 preview, Outlook doesn’t automatically setup @Live email accounts.  I’m guessing this is just an oversight in the preview. To work around this you need to set up the account manually and specify the server as m.hotmail.com

In the Add Account screen select “Manual ....

image

 

In the next screen select Exchange ActiveSync :

image 

Next when adding your account information, specify the server as m.hotmail.com

image

 

Enjoy.

with no comments
Filed under:

Yesterday’s announcement of Windows Phone 8 not being an upgrade for the latest Nokia phones, including Lumia 800, 900 et al, has been met understandably with both disappointment and anger from many existing handset owners. I’ve also seen some articles/comments that suggest it is needed for the greater good; articles which clearly missed what WP8 potentially brings, and clearly missed the point that existing handset owners clearly like what WP8 has to offer: hence their angst.

First of, the question of hardware. Existing Nokia Lumia's use Snapdragon ARM (v7) processors also known as system on a chip (SOC). The new version of Windows Phone will also run Snapdragon ARM chips, also currently ARM V7.  Where’s the massive hardware incompatibility? There’s been silly comments about the NFC chips, but no, the real hardware issue is most likely the graphics drivers. For a closer look, read the Windows 8 team blog on building for ARM.  You’ll see that the Windows 8 team actually did a lot of their early development on existing phones !!  It doesn’t look like it is a hardware issue, rather it’s a question of the investment into updating drivers for existing graphics chips Surprised smile

Secondly, there’s the misnomer that having the new start screen will make Windows Phones 7.5 just like Windows Phone 8. Wrong. WP8 brings with it some very important changes, two of which are native code, and drivers. Drivers will open up a world of possibilities with both existing hardware such as printers and scanners, external drives, external video etc. WP8 will start the era of new connectivity for devices.

Native code will change the games and apps we see for Windows Phone.  Many of the apps Windows Phone users have been waiting to be ported over from the iPhone or Android will be written in native C++ and hence never be seen on the existing devices. New immersive games with stunning graphics also won’t be available for existing devices as long as they are stuck on Windows Phone 7.X

Even the fate of web browsing on the phone is looking bad. With the existing share of Windows Phones being such a small market segment, if the phone doesn’t get the newest IE and HTML 5 support there’s an increasing chance over time that web sites won’t even consider the existing devices and their rendering capabilities a viable target.

Thirdly, the lack of an upgrade for existing phones is going to do severe damage to the Windows Phone market whilst the market waits for WP8 capable devices. This sounds like it is still months and months away. After the recent hype and marketing push for the Nokia Lumia’s this is a massive back step.  Will Microsoft and Nokia be buying back existing devices retailers feel they are stuck with or giving massive rebates (remember the Microsoft Kin anyone?). The real cost to the market is going to be huge.

This black hole of no devices available that will be upgradable to windows 8 is going to strike at the confidence the market place has with Microsoft. It was only three years ago Microsoft abandoned Windows Mobile 6.5 for Windows Phone 7. Now they are abandoning it again. The upgrade policy announced yesterday is for only 18 months (although expect that to be challenged legally in Australia at least where telco’s are required to guarantee products for the minimal term of the contract which is usually 24 months). This adds to the recent history of abandoning Zune devices and phones such as the Microsoft Kin.

Windows On ARM (WOA) which is the shared core for Windows RT (Windows 8 on ARM tablets) and Windows Phone 8 will most likely at last bring a stability to the core for devices. But this is the same OS core that we have to regularly update, virtually every month. A support guarantee for 18 months only isn’t going to really cut it.

Lastly, but certainly not least-ly,  many of the people who have bought Windows Phones have done so knowing the devices won’t upgrade in terms of hardware, but they expected the software to be updated and new applications to be available to them.  Microsoft’s rush to get windows Phone 7 out left it without many features, features people expected to be updated/upgraded over time. The lack of some big name apps was something people thought would be fixed with time as more people bought the Windows Phone. People bought into Windows Phone on a promise of things to come, and did so knowing that the more that bought the phones the more likely we would be to see new apps on the phone.

They bought into trust and faith that things were going to get better. And it might, but not for those who backed Windows Phone 7.

The North American TechEd videos are online now. The keynote from Day 2 is about Windows 8 and is worth watching.

Actually, whilst watching the day 2 keynote see if you can count the times “actually” is used, actually Winking smile       I guess it’s better than “So...”

Biggest moment of irony in the video is when the desktop is demo’ed with windows docked to 50% of the screen. It’s a real pity you can’t window a Windows RT app to do the same.

with no comments
Filed under: , ,

My first impressions of the windows 8 mail application ...

Accounts :

The mail app seemed to work straight away with my @live account, and looks like it will offer good synchronisation with multiple accounts however I am unable to get the “accounts” charm to stay open when running in a VMWare VM, so haven’t tested multiple accounts. The @Live account seemed to cause synchronisation issues for Outlook on another machine which was only resolved by deleting the local ost file.

Likes:

  • Live tiles indicating how many unread emails.
  • Has a clean look

Dislikes:

  • No apparent security: internet pictures are automatically downloaded, and you can’t set it to read html mails in text mode etc.
       
  • Navigation when docked is difficult.  It’s not intuitive to navigate from store, to list of items to an actual email item. Could really do with some breadcrumb or similar to show you where you are
       
  • Can’t display two emails side by side (no “windows”).
        
  • No rules. The ability to automatically move emails from different people isn’t there. This alone is probably the real deal breaker for me, and the number one reason why I won’t be using the mail app.
       
  • Can’t change font or font size. Unbelievable in this day and age that the user can’t decide to change settings such as font size. I don’t believe any app that is truly interested in accessibility would leave this functionality out. Shameful Sad smile
      
  • When composing a new email the formatting toolbar is now at the bottom of the screen. This might make sense if you are using the onscreen keyboard, and hence is okay for tablet use, but for keyboard and mouse input it’s in the wrong place. This epitomizes my major issues with the Windows 8 design: they continually favour tablet touch input design at the cost of mouse and keyboard input design. The ergonomics of having the formatting toolbar at the bottom of the screen are terrible. The only time it should appear there is when you use the on screen keyboard.

Conclusion: I wouldn’t consider this a real email client. Security needs to be addressed before recommending to even the most casual of users. It needs rules. And the way it works in a desktop environment really needs to be re-thought.

Steven Sinofsky posted to the windows 8 team blog on their design for the mail app. Sadly I don’t see these issues addressed there .

with 1 comment(s)
Filed under: , ,

Scott Hanselman posted about VB6 and showed a snippet of code :

Public Function Fib(ByVal n As Integer) As Integer
    Fib = IIf (n < 2, n, Fib(n-1) + Fib(n-2))
End Function

What Scott probably didn’t realize that the IIF function in VB (both VB6 and later) is a Function not an operator. That means all arguments passed to the function are evaluated.

Can you see the adverse side effect Scott’s code would have ?

 

NB. In VB.NET if you wrote the same code as Scott posted, and you have Strict On, the compiler will give you a type casting error because the IIF function is not generic.  So that might help with the unintended bad usage. The better news is VB.NET has an If operator (ternary operator) So the above code is written as :

 

Public Function Fib(ByVal n As Integer) As Integer
     Fib = If (n < 2, n, Fib(n-1) + Fib(n-2))
End Function

 

Generally IIF should be avoided regardless of what version of VB you use.

 

with no comments
Filed under: , , ,

The LightSwitch team announced that LightSwitch will render to HTML5 + javascript. This is huge in expanding and liberating the client requirements including mobile devices !!

see the LightSwitch team blog for more details ..

The software a phone runs is important but the hardware is even more so. My current day to day phone is the Lumia 800, and the previous one was a HTC mozart. To me the difference is massive. The lumia has a much better display, better reception (which is really important to me), and better battery life. Like I said the difference is massive to *me*.

So whilst thinking/talking hardware, here’s some of the updates I’d like to see:

  1. No more charge cables !  I’m sick of plugging in my phone to charge it. Sure there’s those adapters with pins and charge pads, but I’d really like some like inductive charging. If it’s good enough for my toothbrush I’m sure they can make it work for phones Smile
     
  2. NFC. Yes many phones already have this, enabling secure payment. Unlike a credit card you can remotely lock a phone if it lost/stolen. Plus it’s one less thing I have to carry if all cash transactions can be done with the phone

     
  3. Smart key integration. I’m sick of keys. It’d be nice if my phone could replace all my keys, starting with the car keys first.

     
  4. More wireless communications. Apple has some of this, but it would be nice if it was all open standards. I'd like to be able to post my phone screen to the television; wirelessly print and general wireless device communications. We’re very close on this ...

  5. Tougher gorilla glass.

Most of those things are about less clutter, less cables. It’d be nice if I didn’t have to worry about keys, wallet, phone, pager, instead just had to grab one device to do it all Smile  First on my list is get rid of the daily grind of charging it !!!

The following is my response to the VS blog post about the design for all caps in top level menus:

http://blogs.msdn.com/b/visualstudio/archive/2012/06/05/a-design-with-all-caps.aspx

First off, why would you want to add *EMPHASIS* to menus when the design philosophy stated for the new look of VS was it didn't distract from the content we are creating?

Second, the examples provided of Bing, Azure portal and Zune are complete mismatches.

Bing doesn't add top level menus in ALL CAPS, quite the opposite: they are all in proper case. What you see in Bing is all caps used for pane titles. This difference is very important. Bing was more like the first beta of VS 11, not the latest bits. Also note the use of all caps is only about four or five items over the entire screen. VS top level menu is **SEVENTEEN** distinct items!!

Zune uses lower case at top level (quickplay, collection, marketplace), and all caps at the individual pane level, again not totalling more than 5 distinct items on any pane.

Azure portal on the web does use ALL CAPS for the top level menu, but it is only six items and uses a LOT of white space around and between them.

From a visual design perspective the examples given show use of white space, and clarity, whereas the VS 11 menu is cramped and way too busy. You cannot relate fourteen items to designs of five items, especially when the five item designs use copious whitespace, whilst VS menu does not.

It's great we will be able to change this, but sad so much effort is being wasted on a design implementation that really adds no value to VS, contradicts both its own design philosophy and those of windows guidelines.

FAIL.

with no comments
Filed under: , , , ,

Today I has having some discussions over the right roof pitch for a car port, so I grabbed my phone, rotated it and did some quick trigonometry.  Ten minutes later I tried to do the same and I could only see HEX and BIN, not Sin and Tan et al.  I’d forgotten it all depends on whether or not you turn it left or right.

Rotate the calculator to the left for trigonometry functions.

Rotate the calculator to the right for logical/programmer mode.

I’ll try not to forget next time; was embarrassing when I had to get the laptop out to do an inverse tan calculation, after having using the phone for similar calculations just ten minutes earlier Winking smile

The beta of Visual Studio 11 is causing a lot of feedback around the grey shade icons.  The use of colour or not has also brought to the forefront the UI for colour-blind people.  So I thought I’d post a picture of my toolbar, along with some links to see the page as seen by those with colour-blindness...

My toolbar:

image

 

Colour-blind filter web site: http://colorfilter.wickline.org/

This post on the above site:
http://colorfilter.wickline.org/?a=1;r=;l=0;j=1;u=msmvps.com/blogs/bill/archive/2012/03/07/visual-studio-toolbar.aspx;t=p

with no comments
Filed under: , , , , ,

There’s a lot to like in windows 8 preview: it’s easy to see how windows 8 will be a hit on tablet devices; there’s massive synergy with windows phone; the built in windows live experience combined with your settings in the cloud, make it smooth to go from one device to another. But there’s other bits I’m not so fond of ...

The Start Menu

Obviously the biggest change people first see is the start menu is now a start page. I like the concept but dislike the fine details on how it is implemented. At present to get to the start menu you have to move your cursor to the lower left of the screen; to see “running” apps you have to move it to the upper left then swipe down; to see all applications you need to right click to see the “toolbar” along the bottom of the screen and select all apps. It’s those details I don’t like. They aren’t intuitive, they aren’t obvious. People have to go through an initial period of disorientation: that’s a negative first taste for existing customers. What I’d much prefer to see is :

  1. Put the windows start button back: not the start menu, just the button. Call it branding if you like, but having the windows button visible always would make the experience a lot more intuitive
  2. Make the start screen a start portal, not just a start menu replacement. That is, get rid of the silly running application list on the left and instead have the start screen have three or four title style buttons along the top (similar to windows pivot app style) that would give you the choice of Favourites, Running and All. Perhaps even a Most Recent as well. The running app list at the side just doesn’t work well for me. They look too much alike, there’s no captions, and the space is limited. It’d make a lot more sense to use the whole screen for running apps, just like it makes sense to use the entire screen for the start “menu”. Currently there is a disconnect of design.

  3. When in desktop mode the start button would be there (see 1 above). Clicking on it would show you the start screen BUT not in full screen mode. Instead have the start screen appear floating: eg a 20 pixel margin all around and a drop shadow. It would make the experience feel more connected rather than the current disconnect.

  4. Allow metro apps to run in “windows”. That is you should be able to preference any metro app to run inside a window on the desktop. This should be a right click preference from the start screen, and likewise from the running app screen to move apps to and from the desktop windows experience. I think this is important for a lot of existing customers who will find themselves still on the desktop and currently experience a disconnect from the metro start screen.

  5. Better panel docking. It seems a step backwards when you can only dock as a thin pane to the side without being able to resize. This totally failed for me when I tried to have my mail on the left pane and my calendar as the main screen (try it: I couldn’t actually read the email). Panels should be able to be sized, apps need to be able to respond to that better than what’s in the preview, and most importantly you should be able to work with desktop applications (4, above would help solve this for metro and desktop apps working side by side)

Overall I kind of like windows 8: I’d like to like it a lot more but it just doesn't feel as if it is there yet. I think the above list of changes would go a long way to bring in the new experience in a much less discordant way.

with no comments
Filed under:

Hopefully you already know VB has full support for Optional parameters, both declaring them and calling them, but did you know you can use Dates as Optional parameters ?

   Public Sub AddNewCustomer(customer As Customer
                            
Optional dateAdded As Date = Nothing
)
     
If dateAdded = Nothing Then dateAdded = Now

 

VB has had this support for Optional parameters in place for the last decade or so in .NET and for years before that back in the COM versions of VB.

In the last release of .NET (VS 2010), C# finally got support for Optional parameters, so the above in C# would look like:

     public void AddNewCustomer(Customer customer, 
                          
DateTime dateAdded = default(DateTime
))
      {
        
if (dateAdded == default(DateTime)) dateAdded = DateTime.Now;

 

The Nothing in the VB declaration is the same as default(DateTime) in C#, which equates to the theoretical Gregorian date of 1/1/0001.

But what if you want to specify a default date ? Well because VB supports date literals, in VB you can, but in C# you can’t.  In VB you can write any date literal for the optional parameter value. For example, you might have some legacy database support, and when the date is unknown you want to use the equivalent of an OLE Date’s zero value:

  Public Sub AddNewCustomer(customer As Customer,
                            
Optional dateAdded As Date = #12/30/1899#)

 

When the VB compiler compiles this, it adds a System.Runtime.CompilerServices.DateTimeConstantAttribute to the parameter information with the default value stored as an Int64 (the number of ticks). C# actually sees the optional value in the above example and will use it. That is both VB and C# can call the code taking advantage of the optional value for the date parameter, but only VB let’s you define that value.

From time to time there’s discussion about the way dates are displayed in the Visual Studio IDE for Visual Basic. Typically dates are shown using VB’s date literal syntax of #MM/dd/yyyy# which is the standard US format.  For people outside of the USA this can be confusing or ambiguous at times.  The good news is Visual Studio allows you to easily add your own display formatter.

Simply create a new class library project, add the following attribute to your AssemblyInfo file:

<Assembly: DebuggerDisplay("Date: {ToString(""s"")}   kind={Kind}", Target:=GetType(DateTime))>

And then just copy the assembly to your Visualizers directory, eg:
  My Documents\Visual Studio 2010\Visualizers

I added the Kind property to the display so as you can easily see if the Date is a local date, UTC or unspecified.

I've attached a sample project.  Enjoy Smile

 

The DateTime structure in .NET includes custom operators for Date comparisons such as less than, greater than, equal and not equal; but did you know it also includes addition and subtraction operators ?  There’s two subtraction operators and one addition operator defined inside DateTime:

  • date = date – timespan
  • timespan = date – date
  • date = date + timespan

Those three cases should all seem relatively obvious, such as adding or subtracting an hour from a date, or calculating how many minutes between two dates etc...

BUT, did you know VB also defines another case where you can add two dates, eg:

     Dim result = Date1 + Date2

Now if you guessed that the type of result is String, then congratulations !! Note: this operator resolution is only valid with Option Strict Off

Obviously adding two dates can’t give a valid date or timespan, so the only real options are to return a string concatenation of the date or throw an error. There are a couple of fringe cases where, depending on your system locale, the resultant string can indeed be parsed back into a date, eg:

      Dim dt1 = #1:00:00 PM#
     
Dim
dt2 = Now.Date
     
Dim dtResult As Date = dt1 + dt2

But that really is a fringe case, and would depend on the system locale formatting.

I think it’s probably fair to say this case is a quirk of a quark, probably introduced as partial legacy. I honestly can’t see any usefulness of the addition operator between two dates that returns a string. If the intent was some legacy support, it might have made sense to return a date if one of the dates was date zero with time information, but the concatenation of the date strings doesn’t seem to reliably serve any purpose.

The good news is you can only do the string = date + date operation with Option Strict Off. Guess that’s just another reason to turn Option Strict On  Winking smile

Can you pick the problem with this code ? :

      Dim currentChar As Char

      For i = 0 To
largenumber
         currentChar = getChar(i)
        
If currentChar = "a" Then
            count += 1
        
End If
     
Next

 

The answer of course is the literal “a” is of type string, not of type Char. Hence currentChar gets implicitly widened to a String to make the expression a string comparison expression. This means a new string is created with the contents of currentChar on each iteration, and the comparison is a string comparison which checks for null strings and empty strings and then does a compare ordinal etc. This is incredibly inefficient.

If “a” was typed as Char, then the comparison is a simple low level IL compare of the underlying values (int16’s).

You can use CChar, as in CChar(“a”), but it’s a lot easier just to add the type literal suffix c, eg “a”c

      Dim currentChar As Char

      For i = 0 To
largenumber
         currentChar = getChar(i)
        
If currentChar = "a"c Then
            count += 1
        
End If
     
Next

The change in this code is about 20 fold performance improvement (run as release build outside of Visual Studio). That’s a 20 fold increase just by typing one extra letter !!

This example was based on real sample code where a massive file was being parsed char by char.  There too, attention to the fine detail showed a massive performance improvement over the original code. It’s often the little things that can make a huge difference.

 

This VB quark was brought to you by the type literal “c”

Do you know why you can’t write this code in VB:

   Dim x = 123456789.0123456789

Answer: The IDE won’t let you Winking smile

If you try to write that code the IDE will truncate the number, giving :

   Dim x = 123456789.01234567

To include all the decimal places you need to be using the Decimal type. To do this you indicate the constant is of type decimal by adding the suffix D on the end, eg:

   Dim x = 123456789.0123456789D

The reason for this is VB treats numeric literals as of type Double by default if they have a decimal place. If there is no decimal place in the literal then the value is consider to be an Integer (Int32), or if it is too large for an Integer, it’s considered to be a Long (Int64).

If you want the literal as a type other than Double, Integer or Long you need to either convert using CType, CShort, CSng etc, or add a type literal suffix. The following is the complete list for numeric type literal suffixes in VB:

Suffix Type
F Single
R Double
D Decimal
S Int16, Short
I Int32, Integer
L Int64, Long
US UInt16, UShort
UI UInt32, UInteger
UL UInt64, ULong

In case you’re wondering, the “F” for Single is short for "Float”, and the “R” for Double is short for “Real Number”.

Here’s another example of where you should use type literal suffixes. Consider this code :

      Dim i1, i2 As Int16
      i1 = &HF0
      i2 = i1
And &HFF

If you have Option Strict On, the last line will cause a compile time error saying that an implicit conversion from Integer to Short is not allowed. So what is happening here, and why doesn’t the middle line cause the same problem ?

Well the line i1 = &HF0 compiles fine because the constant &HF0 can be evaluated at compile time, so there’s no problem there. The last line however cannot be evaluated because the variable i1 is not a constant. Try it for yourself if you like: change the declaration of i1 to a Const, and you’ll see the last line  compiles fine with Strict On.

Because VB can’t evaluate the variable expression at compile time, it uses the default type for numeric constants : Integer (aka Int32). And because a short can be implicitly widened to Int32, the expression on the right hand side becomes an Int32: hence the error.

In Visual Studio the error correction wizard will suggest converting the entire  expression to an Int16, as such :

      i2 = CShort(i1 And &HFF)

But that’s a runtime conversion that isn’t needed if you declare the &HFF constant with the correct type literal suffix S:

      i2 = i1 And &HFFS

As you can see, the code correction wizard in Visual Studio doesn’t always provide the best syntax to use, so it really is up to you to know the correct literals to use and apply them. 

Oh, you may have also noticed there is no type literal suffix for Byte and SByte. I’ll leave that for yet another quark Smile

Can you spot the problem with this code:

   <Extension()>
  
Public Function ToColor(argb As UInteger) As Color
      Return Color
.FromArgb( _
                     
CType((argb & &HFF000000) >> &H18, Byte
), _
                     
CType((argb & &HFF0000) >> &H10, Byte
), _
                     
CType((argb & &HFF00) >> 8, Byte
), _
                     
CType(argb & &HFF, Byte
))
  
End 
Function

 

If you said it should be the bitwise And operator not the string concatenation operator give yourself a pat on the back.  The problem here is three fold:

  1. The code is a failed attempt at translating from C#, and
  2. The project must have Option Strict Off for this code to even compile.
  3. No testing or unit tests to check if the code  works at runtime.

Sadly this is all too common. The above is in fact a very real example taken from the Silverlight Toolkit samples on Codeplex .

So the steps to correct this are first be aware of the language equivalents when translating from C# to VB. See the MSDN documentation on language equivalents for a great starting point. And take note of the operator equivalents. Some of the most common operators where people seem to make mistakes are:

operator VB C#
bitwise And And &
bitwise Or Or |
bitwise exclusive Or XOr ^
Short circuited Boolean And AndAlso &&
Short circuited Boolean Or OrElse ||
equality = ==
assignment = =
not equal <> !=
exponent ^  
modulus Mod %
string concatenation & +

From the short list above you can see the ^ and & operators have very different meanings in VB compared to C#. Which leads us to items 2 & 3.

If you are unsure of VB, or are translating and hence have a lot of code that *may* be invalid you can improve catching of these kind of developer mistakes by turning Option Strict On. In the original example, the argb & &HFF000000 would have given the result of a string: argb.ToString followed by –16777216. At runtime this string would then try to be converted to a number and then shifted right, most likely failing.

So if you turn Option Strict On, you’d immediately get warnings on trying to convert the string to a long. But even once you’ve got it compiling with Strict On, you should test the code. A lot of code you can quickly query in the immediate window, or write unit tests.

Oh, and the corrected code in this particular case is:


   <Extension()>
  
Public Function ToColor(argb As UInteger) As Color
      Return Color
.FromArgb( _
                     
CType((argb And &HFF000000) >> 24, Byte
), _
                     
CType((argb And &HFF0000) >> 16, Byte
), _
                     
CType((argb And &HFF00) >> 8, Byte
), _
                     
CType(argb And &HFF, Byte
))
  
End 
Function

 

For a complete project that fixes the VB samples for the Silverlight Toolkit, see my blog post from last month

More Posts Next page »