### VB Quark #2: compiler directives and constants

Building upon VB Quark #1 , did you know you can use compiler directives alongside expressions in constants ?

#If CONFIG = "Debug" Then
Const path As String = "Z:\mydebug.sdf"
#Else
Const path As String = "|DataDirectory|\Database1.sdf"
#End If

Const connectionString = "Data Source=" & path

The nice thing about this is as you change the config from Debug to Release, Visual Studio will grey out the parts that aren’t in the current compilation. In the above example, when I do a debug build I’m using my database in Z:\mydebug.sdf, but when I do a release build it automatically uses the database Database1.sdf in the application’s DataDirectory.

You can use compiler directives pretty much anywhere in your code to replace any code block with another at compile time based on the directives.

### VB Quark #1: constant expressions

This quark is more of a did ya know quark about constant expressions in VB. You probably know you can declare a constant expression such :

Const appName As String = "My Really Cool App"

But did you know you can also do operations in constants ?

Rather than calculate the area of a circle you can write:

Const area = Math.PI * radius ^ 2

The compiler will calculate the area constant at compile time. This makes it easy to change the area constant by simply changing the radius.

The operations you can do include all the standard mathematical operations : +, –, /, \, * , ^ and Mod as well as bitwise operations such as And and Or as well as bit shifting << and >>.

For example you can declare an enum for bits using shift operations: (note: Enums are constants)

Public Enum bits
bit0 = 1
bit1 = 1 << 1
bit2 = 1 << 2
bit3 = bit2 << 1

End
Enum

You can also use the If operator. For example you might want to limit a constant based on another constant, eg if gravity is greater than 20g’s then you might want to limit it to 20g for calculations.

Const workingG = If(g < 20 * 9.8, g, 20 * 9.8)

And you can also do conversions between the intrinsic numeric types, eg CDbl, CSng, CDec, CLng, CInt, CShort, CByte, CULng,  CUInt, CUShort, CSByte. And conversions to and from boolean to the numeric types: CBool and the aforementioned conversion operators.

Const roughArea As Int32 = CInt(area)

For dates, there aren’t any operations or conversions you can do: you have to define them using the literal representation in US format, eg today 24/9/2011 is defined as #9/24/2011# . This is still a lot better than certain other languages (not picking on C#) that don’t support constant date literals.

For strings, you can’t do any conversions but can use the concatenation operator &. You can use + instead of &, but I don’t recommend that (more on that in another quark). Strings that are compiled as constants are also typically interned.

Anywhere you use an expression that can be compiled as a constant, it is evaluated at compile time: you don’t have to declare a constant variable to reap the benefits of compile time evaluation.

### VB Quark #0 : lambda expressions and statements

This VB Quark is the one that started the conversation some weeks back. It’s number 0, both because these days things are typically 0 based in .NET, and also you could say it is ground zero. It’s about a quirk quark to do with lambda functions versus lambda subs.

Let’s say you have a Customer class that has an Approved property :

Class Customer
Public Property Approved As
Boolean
End

Class

Now if you were to write the following code on a list of Customer, what would you expect to happen ?

customers.ForEach(Function(cust) cust.Approved = True)

If you said nothing, you’d be pretty close to the answer.

The quirk quark here is two fold. First of, ForEach expects an Action(Of T) to be passed to it. Action(Of T) in this case has the form of:

Sub DoAction(cust As Customer)

‘code here
End
Sub

But note we passed in a Function. Function’s have return types. In this case the return type is a Boolean implied from the expression cust .Approved =True . That is the = operator is an equality operation in this case, not an assignment operator. The original expression is seen by the compiler as the equivalent of :

customers.ForEach(Function(cust)

Return cust.Approved = True
End Function)

If you actually want to set the IsApproved values in the ForEach you can simply use a Sub instead of a Function:

customers.ForEach(Sub(cust) cust.Approved = True)

So the quarks to remember here are the = operator in VB can be either equality test or assignment depending on the context; and not all lambdas are lambda expressions (aka Functions),  some can be lambda statements (aka Sub)

I’ve seen this quark trip up a couple of really smart people, usually when they are translating code from C# to VB. Apparently some translation tools also make the same mistake.

### VB Quarks …

A few weeks ago I was having a discussion about a particular language feature that was causing a couple of people to trip up in their code. From that discussion we identified some places where the VB compiler and IDE can possibly help in the future; but for today there are some language features that often go unseen but combined do matter (geek pun intended).

So I’ve decided to put together a series of posts I’m calling VB Quarks.  The things I’ll be covering are your gotchya’s, the easy to forget things, the little things that combined matter (oh sorry same sad pun again ).

### Windows Phone Mango and multiple google calendars

With Windows Phone 7.5 (aka Mango) being released in the next couple of weeks, you may have to work around a google limitation to get multiple google calendars on your device. Both Windows Phone 7.5 and google calendar allow you to have multiple calendars on your phone, but for whatever reason Google doesn’t let you see how to set that unless you trick it to think you’re using an iPad or iPhone. (shame on you google )

To trick google, you just need to change your browser user agent string. The steps are:

1. setup your google account on your phone as normal and sync with calendar and email. This will give you only the one google calendar for that account.
2. Next open up IE 9 on your desktop or laptop computer, press F12 to bring up the IE developer tools. Click on the developer tools Tools menu and select Change user agent string –> Custom

Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.10

### VB Windows Phone Toolkit Aug 11 sample

The Silverlight Windows Phone Toolkit August 2011 (7.1 SDK) doesn’t contain a working VB sample.  If you try to load the VB sample that does ship, you’ll get hundreds of errors.  So I put together the VB sample that should have shipped.

Enjoy

Oh, and Thanks to Nick Randolph for confirming the SDK doesn’t contain a working example.

### Remember variant ?

Cory is continuing his posts about VB10 differences from VB6. His latest posts is on variants.

Variants were/are basically a structure that contains a variant type enumeration and either the variable’s data value or a pointer to the variable data. In pseudo code it basically looks like:

Structure Variant
Public VT as VariantType
Public Value As Object
End Structure

The Value field would hold the actual data in the case of simple value types, or a  pointer to the object such as in the case of arrays. In the .NET world there are actually variant structures but not as part of the .Net framework: you can find one for example in the Microsoft.VisualStudio.Package namespace.

The reason you won’t find it in the framework is because the variant structure is not only limited but also superfluous in .NET due to the type system. Take for example the case of a value as a Double versus an Integer. In VB6 you would use Variant to allow you to pass both values to a method. The Value field would hold the actual data so it was important that the VT field told you the value type. The problem with this approach is the VT field is an enumeration, so doesn’t allow for custom value types to have their actual type discoverable: try passing a UDT into a variant and then determining which type of UDT is is in VB6 to see what I mean

In .NET the object based type system means you can always determine the type of a variable. So the “everything stems from Object” approach in .NET is definitely more versatile.

There was however one thing missing: yep, IsMissing was missing. Variants allow optional parameters to indicate if a value was missing. The first release of .Net didn’t have anything in place to indicate if an optional parameter was missing. It wasn’t until nullable types were added that you could do the equivalent of IsMissing by testing for Is Nothing. The Is Nothing approach is more holistic and uniform (as opposed to IsEmpty versus IsMissing et al) and works across the board for nullable types and As Object variables and parameters.

So, in summary, there was initially some gains and some losses with the move from Variant to an Object based system, but as the .NET framework and language implementations have matured, the Object based system (.NET) offers far more functionality and flexibility.

Oh, as you can probably see, the above explanation/description actually touches on a number of items in the list Cory referred to: in particular but not limited to items 11 and 27. I think it is always important to come up for air and see the big picture: the forest from the trees. The overall benefits of moving to Object based in .NET extend far beyond these one or two items.

### a little help for volunteers

Today Microsoft announced an even more generous program designed to help non-profit organisations !!

http://blogs.technet.com/b/microsoftupblog/archive/2011/07/26/expanding-microsoft-software-donations-to-more-nonprofits.aspx

Kudos guys!!!!

Thanks

### Great Ocean Walk photos added

Last weekend we strolled around Glen Aire, along the Great Ocean Walk from Castle Cove to Johanna:

### Dell XPS 17 (L702x) review

I recently bought a Dell XPS17 (L702x) and thought I’d post a review for those considering purchasing this notebook. There’s a massive thread about all things good and bad in relation to this notebook over on notebook forums; the following is just my opinion

Weight : good for the size.

Screen: Love it !!  I have the anti-glare screen (1920 x 1080) and it’s a lovely screen. I did make minor adjustments to the gamma, toned the blue down a bit and the green just a tad. Photos, especially nature scenes, look fantastic, true to life colours. The antiglare is also really good. Compared side by side with a glossy screen laptop you really appreciate how well it works: no  annoying reflections of windows etc. Viewing angle is really wide with little distortion.

Keyboard: It’s growing on me, but i think it could be better. I like the back light. Problem with the keyboard being a full keyboard is it means centre of the keyboard is off centre, which means i tend to have the laptop shifted out to my right. Given how much space there is I think Dell could have done better with keyboard options, such as offer a comfort curve option.

Touchpad: It’s big. Also off to the left a bit so as to almost be centre of the alpha part of the keyboard, but not quite. Centre of the touchpad lines up with key 7 or the space between Y and U. Gestures work pretty well: I particularly like the cover the touchpad to hide all open windows gesture.

RAM is 8 GB which is good enough for me (generally). Unfortunately only 2 slots. Apparently if you order the 3D screen you get 4 slots. That kind-of sucks, but i doubt it will bother me in the next couple of years.

Processor: i7 2630QM. Seems good and fast. Yet to really multi thread stress test it though.

Graphics: most of the time I’m using the Intel one (or so it seems). I’m not a gamer

USB: Awesome !! My external HD which is USB 3, is fast   I also really like the charge port for charging my phone.

Battery: I got the 9 cell. i like how it sticks out of the bottom to tilt the notebook when sitting at a table. Battery life seems good. I use the machine for hours and still have half battery left.

Hard drives: There’s two bays and came with two 750 GB @ 7200 RPM. I found them relatively noisy and kind-of slow, so I put in one of my old solid state drives and also added a new OCZ Vertex 3. The Vertex 3 uses the 6 Gbs bus and it rocks !! Lightning fast, and absolutely no noise. There was a problem with the notebook hanging for anywhere up to 30 seconds or so. Looking in the windows log I identified the problem as being the Intel SATA drivers so I uninstalled them, and since then it has been rock solid (and very fast )

Sadly the Dell web site doesn’t really give much choice on this: they don’t tell you what kind of Solid State Drive they offer as far as performance goes, and they only offer pricey options of 256 GB or more. Given this notebook has two internal drive bays, having two @120 GB would seem the best bang for your buck at present. My OCZ HD is getting the maximum windows experience index score of 7.9:

Overall opinion: I like it .