In case you’ve missed it, Lucian Wishick from the VB team is doing a set of blog posts about feature ideas for future versions of VB (VB11 and later). Lucian assures me **ALL** of the ideas in my previous wish list for VB10 are on the list !!!!
Now’s a great time to speak up and discuss these and other features you might have in mind.
There’s a new function in VB 10 called CTypeDynamic .
(note it is a function not a keyword so the Microsoft.VisualBasic namespace needs to be imported)
The CTypeDynamic function looks for dynamic information and performs the cast/conversion appropriately. This is different from the CType operator which looks for static information at compile time or relies on the types being IConvertible etc. For example, consider the following simple class with a custom narrowing operator to String:
Class Foo
Public Name As String
Public Shared Narrowing Operator CType(ByVal instance As Foo) As String
Return instance.Name
End Operator
End Class
If you were to create an object instance of Foo, you’d find you couldn’t cast it to string with CType. This code fails:
Dim f As Object = New Foo() With {.Name = "Bar"}
Dim s As String = CType(f, String)
But if you use CTypeDynamic the code works:
Dim f As Object = New Foo() With {.Name = "Bar"}
Dim s As String = CTypeDynamic(Of String)(f)
The difference is the CTypeDynamic examines the object at runtime including looking for Shared (aka static) custom operators. The CType operator would only work in this case if f was explicitly dimensioned as type Foo or cast to it. That is, for CType to work with custom operators the type of the original object must be expressed in code before calling CType, eg:
Dim s As String = CType(DirectCast(f, Foo), String)
CTypeDynamic on the other hand does the work at runtime. This obviously comes at a cost as it must examine the type(s) at runtime. Hence the “Dynamic” part in it’s name. If you know the type when writing the code, use CType, but if you need dynamic casting then you need to use CTypeDynamic.
See also “fun with dynamic objects” blog post by Doug Rothaus.
.
If it wasn’t true, a software bug in EFTPOS machines from the Bank Of Queensland (and others) would be pretty funny. Problem is *most likely* they are treating the year as a two digit date (watch out come 2100 !!!), so today with the year being 2010, they read the year as 10 and make that as 16. !!! That’s right they make 10 to be 16. A lot of you are probably saying “what the ???”
But if you consider Hex notation, eg &H10, or 0x10, well then that 10 is in fact 16. Seems the bank has been hex’ed ;)
Well the verdict is in, the reason I couldn’t read the bottle of beer label (amongst other things) is due to presbyopia… or in other words kind of average for someone in their forties ;)
The good news is my eyes are only slightly different in strength from each other so looks like I can get by with off the shelf 1.00’s for now.
If I can’t read the small print on a bottle of beer does that mean I need glasses ??????
VB 9 or later has a particular feature that can make life easier in some circumstances: the compiler allows you to access indexed items on non indexed enumerables. Let’s say you have code such as:
Dim items as IEnumerable(Of String)
. . . .
For i = 0 to items.Count - 1
Debug.Print items(i)
Next
VB will compile that for you. The Count is an extension method that loops through the list to determine the count; luckily VB calls that only once. The real killer though is items(i) is actually compiled as items.ElementAtOrDefault(i).
The ElementAtOrDefault extension tries to cast the IEnumerable(Of T) to an IList(Of T); if the IEnumerable isn’t actually an IList, it loops through the collection, returning at the given index. As you can imagine this would get extremely slow for large lists especially inside a loop.
The Good:
If your IEnumerable is in fact an IList, such as a List(Of T), then the compiler magically adding this extension call makes life a little easier as you don’t have to cast to IList yourself.
The BAD:
You have to be really careful when doing any code maintenance, especially if you change an IEnumerable source to a different collection base that doesn’t implement IList(Of T). You can end up writing really bad, slow code really easily.
I got bitten by the bad, but I caught it immediately as I was curious about the collection base I was using. In my particular case I had swapped an List(Of T) out with a ConcurrentBag(Of T). ConcurrentBag(Of T) basically uses linked lists where each item is stored as a node. Each node has a previous and next node; the list has a head and a tail. I thought it strange that ConcurrentBag would have an indexer given that would force walking through the nodes. I went to change the items(i) to items.Item(i) and BINGO it wouldn’t compile.
Possible best practices to avoid being bitten by this:
(1) Use For Each loops, not indexers. Your code will be far more flexible/manageable
(2) If you only want the first item, make it explicit by using First or FirstOrDefault extensions.
(3) Try to do the explicit cast to IList(Of T) yourself and work with an IList(Of T) instead of an IEnumerable(Of T)
(4) Consider explicitly using the Item property , eg: items.Item(0) instead of items(0)
I’m not really comfortable with having to use the Item property: hopefully practices 1 to 3 will alleviate the need for practice 4.
.
I got an email from a reader last week asking about saving customisations in Visual Studio:
Hi Bill
I read an excellent article by you
http://visualstudiomagazine.com/Articles/2007/12/01/Customize-the-VB-IDE.aspx?Page=1
Just wondering if you know of a way to export those customisations so I can set up my computer at work restore after HDD fails and share my favorite settings with friends.
Also would like to do same for my VBIDE customisations
I have googled to no avail...... :-/
Well the good news is this is incredibly easy. On the Tools menu in Visual Studio you should see the “Import and Export Savings . . .” command. You can choose what groups of settings to import or export.
This seems so incredibly long overdue, but at last as of .NET 4, IntPtr has + and – operators added to it !!
This means you can now easily write code such as :
Dim ptr As IntPtr
. . . .
ptr += 4
this is great when dealing with offsets etc.
The actual implementation is kind of interesting. Here I’m seeing the implementation as :
Public Shared Operator -(ByVal pointer As IntPtr, ByVal offset As Integer) As IntPtr
Return New IntPtr((pointer.ToInt32 - offset))
End Function
I guess this is because it is the 32 bit version of the library. Hopefully in the 64 bit version it calls on IntPtr.ToInt64 ;)
If you’ve got Windows 7 installed, the Windows Virtual PC for Windows 7 is now on MSDN subscriber downloads !!!
I had the Release Candidate (RC) installed, so had to uninstall that first and reboot before installing the RTM release. All worked perfectly, and my VPC’s from the RC also are working fine :)
If you need to uninstall the RC you’ll find it listed under the “Programs and Features” window from Control Panel. Click on “View installed updates” top left of window, and you’ll find it listed under “Microsoft Windows”
Last night I accidentally pulled out the wrong power plugs, crashing my computer. For some reason this meant it wouldn’t start properly. I don’t quite get why that would mean it wouldn’t start as the hard disc didn’t fail, but none the less it wouldn’t : the computer would seemingly just hang and the keyboard was no longer responsive and its lights went out. The good news is that start-up offered a recovery, and that recovery fixed it and life is good again. The thing to be aware of though is the recovery boot seemed to be hung for a long long time ; I’m talking like 20 minutes or half an hour.
If this happens to you, just be patient: it worked for me :)
First the good news :
The February release of the Snippet Editor has now had 10,000 downloads !
Now for bug fix news:
There were a couple of issues with the paths per language. A problem with Express Editions of Visual Studio occurred due to partial entries in the registry I didn’t for-see. Initial design and testing was done with full versions of Visual Studio, but I want to ensure that it does work with the express versions, that’s why the tool is standalone not an add-in. So the good news is I have fixed those bugs (I think).
If you want those fixes you can use the original source from Feb and just download the updated products.vb file. I’ll probably look at rolling this up into a new release in the not to distant future.
Enjoy :)
Well if you mark your methods as being void from the outset …….
I managed to spend an hour in the veggie patch this afternoon. Was pulling a lot of big weeds really easily after the rain, getting the beds ready for spring :)
And in amongst the weeds I found all these carrots which had self sown from my seed collecting last season:

And they taste great !!! :)
The first signs of spring have sprung:
With the wild plums currently showing the most early blooms :
There’s not a lot happening in the veggie patch, just some carrots and silverbeet mainly. One of the climbers around the garden is also in bloom (you can see some silver beet that was used for seed behind it):
The wattles are in stunning bloom at present:




Blackwood beginning to bloom:
And of course the proteas are also beginning to add some colour
pink proteas:
honey proteas
young king proteas
Recently I got this email from Ted:
Hi Bill!
Love the snippet tool, it's great!
I'm curious, what tool did you use to produce the screencast for the tool? The quality is awesome!
Thanks a lot!
Kind regards,
Ted
The answer to that is I used Camtasia Studio 5. It’s really easy to use and lots of options on output formats. And it will even produce the basic html file for you with the embedded video etc.
Note: I really like Camtasia. I do also get it for free from the nice folks at TechSmith as they value the contribution MVPs make to the community, and that makes it even easier to like :)
An alternative worth looking at is Expression Encoder 3 Screen Capture which comes with Microsoft Expression 3. I’ve only given it a quick test drive and it seemed pretty good. Picking formats was a little daunting, but I got it to produce some good quality wmv files that were really good for the bandwidth (small file size). I haven’t driven it enough to find how to produce web files that embed the video; my guess is it would be more silverlight orientated. Still if you have a MSDN subscription that includes expression, it’s definitely worth a look at.
The last couple of weeks I’ve had what seems to be both a cold and the flu. In the process I learnt some things about them …
First off, they spread easily. We had a two weekend fire line leadership course, running from Friday after work till Sunday midday both weekends. On the first week there was one person with a cold, and another with what was probably the flu. By the following week another six members of the class of twenty three had signs of a cold or similar.
Me, I thought I had caught a cold the first week, and had the usual running nose symptoms etc. Then when I got back from the second weekend I got hit hard with a fever. That seemed to knock the cold symptoms away, but what I had left was the flu as characterised by strong fevers, complete lack of energy and muscle aches. In my particular case my thighs and calves were like they were burning really really hot. I now know first hand the difference between cold and flu ;)
After the first night of fever I thought I was pretty much right or at least getting better for the next couple of days only to be hit by fever again. Foolishly I though that was it and was out and about again the next day. That night the real fever hit and hit hard. My temp was at 40C for hours (I think that’s 104F in old talk). Probably the scary part of that was I wasn’t really sweating. The next day it was hovering more around 39, and at times some profuse sweating which at least meant things were still working ;) All up it knocked me out for the best part of three days. Btu I did learn about safe temperatures.
- Above 40, it’s hospital time.
- 39 to 40: as long as it is fever it’s okay, just keep up fluids an try to reduce the temp a bit.
NOTE: if it is heat stroke this temp is too high and must be cooled. - 38 to 39 fever. Normally nothing to worry about
- 37 to 38. slightly warm (above 37.5 is considered mild fever, but is more like flushed IMO)
So that’s been my fun for the last week or so. One cool thing was I upgraded my main machine from Vista x64 to Windows 7 x64 whilst I wasn’t doing anything (more on that later).
I’m still catching up with emails, may take me a day or two more yet.
With all the focus on Windows 7 RTM announcements, the availability of Expression 3 on the MSDN site last Thursday may have sneaked under folks radars. For MSND subscribers, grab your copy of Expression 3.
The user experience of Live Maps completely sucks compared to Google Maps. It’s that bad I really don’t think they even bother testing it.
Live Maps experience:
- go to maps.live.com. Here it redirects to http://maps.live.com.au/? which seems to fail to properly load every single time. Instead you have to hit refresh.
- the page loads with a script error :
Message: 'dapMgr' is undefined
Line: 480
Char: 9
Code: 0
URI: http://network.ninemsn.com.au/share/js/spac_adx.js
- as I scroll to zoom, the zoom ignores where my mouse is, meaning I have to often drag the map to centre it before continuing zooming
- once I have zoomed in, the map lacks allotment boundary details.
- the “aerial” view seems a lot older and a lot less detail compared to google
- there’s no context menu !! I can’t right click and add a pushpin or get directions form or to there, instead I have to type in an address (how utterly lame !!)
- when I click on directions so as I can add an address, the map goes back to a view of Australia, meaning I lost all that zooming and scrolling I just did.
- If I can’t specify an address I’m rooted (as opposed to being routed <g>). So I guess nearby addresses but then aren’t able to move them.
- I can’t re-route directions, instead I have to try to add stops.
- I can’t hide that directions pane
Google Maps experience:
- No redirection. I get to choose if I use the .au one or not !!
- Pages load first time without any script errors !! (OMG what were they thinking. How dare they actually test it !!)
- As I zoom in, the zoom centres on my mouse and gives me a visual indication it is doing that. (a responsive UI. What the ???)
- the maps show lots of details including allotment boundaries. Aerial view is really good.
- I can right click. OMG, a “context menu” : this app must have been written in the last 15 years !
- I can add pushpins for directions From here and directions To here
- I can re-route the journey simply by dragging the route line. It seems these guys have heard of a mouse !!
- I can hide the directions pane
That’s just a brief synopsis of the very obvious user experience. I found Live Maps totally useless for planning my afternoon’s bike ride. Seriously Microsoft if you are going to go to the bother and expense of licensing the maps and aerial images, of setting up servers etc, then you can at least try to compete. At present it’s just a pathetic waste of money. Say “yes you can” or go home.
A couple of months ago I blogged about iterators in VB (or the lack there-of), and pointed folks to an article I wrote for Visual Studio magazine that provides snippets and templates to help with iterators in VB.
One of the things I talk about in that article is often the use of iterators in C# code that I have seen is superfulous, especially given the LINQ libraries. Yesterday I read another example of this where the developer(s) had written a custom iterator instead of using a LINQ query. Their code required the defining of a generic class:
public class EnumerableGeneric<TClass, TInterface>
: IEnumerable<TInterface> where TClass : TInterface
{
private IList<TClass> list;
public EnumerableGeneric(IList<TClass> list)
{
this.list = list;
}
public IEnumerator<TInterface> GetEnumerator()
{
foreach (TClass item in list)
{
yield return item;
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
}
And the example of using this required the calling code to instantiate an instance of this class:
MyMethod(new EnumerableGeneric<ClassA, IClassInterface>(caInstance));
A simpler alternative is to actually use LINQ, eg:
MyMethod(caInstance.Cast<IClassInterface>());
In VB talk I think it’s even more natural flowing:
MyMethod(caInstance.Cast(Of IClassInterface))
It is in places like that I like the (Of T) syntax of VB a lot better, but some folks prefer a Cup<T> to a Cup(Of T) . The key point here is the use of “yield return” in C# is a good indicator that the code can often be replaced with far simpler LINQ constructs that reduce your LOC’s, and hence reduce your debugging and maintenance loads. There will of course be times where there isn’t a simple LINQ replacement, but if you do ever come across custom iterators, do take pause to think about using LINQ.
Just a quick update. Lots of tomatoes at present. Picking a tray full every 3 days or so.


More Posts
Next page »