Category Archives: VB

Looking for a serious programming book that has VB code samples? Check out this list

While I will always promote the value of being able to read C# and mentally convert it to VB  and being able to read VB and mentally convert it to C# is a skill I think all VB and C# developers should try to have, admittedly, having to do it with a whole book does sometimes get tiresome.

If you are a VB programmer and constantly plagued with having to translate from C# when you are reading advanced programming books, this list is for you.

Chris Williams has just added a page to his I LOVE VB.net website called Serious VB Booklist.

These are not books filled with Hello World samples.

He is just building up the list now, so it’s light. That doesn’t mean there aren’t very many published, just not very many on his list yet. Let him know if you have any additions.

Mutable and immutable anonymous types in VB

Bill McCarthy explained this to me in an email.

He wrote about it in this excellent article in Visual Studio Magazine “Drill Down on Anonymous Types“.

But it still bit me in the ass!

So I am stamping this into my memory:

in a queryresults in
…select cust.CustomerID.cust.CompanyName…  Immutable anonymous type
…select New With {
                                .CustomerID=cust.CustomerID,
                                .CompanyName=cust.CompanyName} …
Mutable anonymous type
…select New With {
                             Key .CustomerID=cust.CustomerID,
                             Key.CompanyName=cust.CompanyName} …
Immutable anonymous type
Direct creation of object 
New With {
                 .CustomerID=cust.CustomerID,
                 .CompanyName=cust.CompanyName}
Mutable anonymous type
New With {
                  Key .CustomerID=cust.CustomerID,
                  Key .CompanyName=cust.CompanyName}
Immutable anonymous type

And just in case someone who really needs this stumbles on this post, two clarifications:

  1. All anonymous types are immutable in C#.
  2. Mutable means changeable, editable. Immutable means written in stone.

Don Box: VB Evangelist?

Last spring, I ran into Don Box in the hallways at MIX07.

I hadn’t seen him in quite a while and asked what he had been up to lately in his wizard’s tower.

Though he couldn’t really talk about what he was doing, he did say that he was working on some cool new stuff and having fun.

I had a quick fantasy about suggesting in a blog post that perhaps Don was working on the next version of Visual Basic, which would have been, of course, a completely insane idea. Don has a bit of a history with VB and I have a bit of a history of calling him out for public jibes at VB (which were likely more innocent than they sounded….).

On the other hand, it was definitely painful to sit in a WCF session a number of years ago where he had been clearly coerced to code in VB (after he was swayed to actually use Visual Studio rather than emacs in public ;-)), though the audience was composed of about 95% C# programmers. I felt badly for him, though he managed to have a little fun with it.

Again, it was no more than a silly idea that crossed my mind, provide me with a momentary evil grin and then it was forgotten, because it could so easily be taken out of context.

Who knew my silly little idea was closer to the truth than I could ever have dreamed? Not to suggest that he is, in fact, working on VBX, but he certainly is whistling (or is that singing) a different tune when it comes to VB these days.

Check out this Channel 9 video with Don Box, Chris Anderson and Amanda Silver!

You can read more of Don’s new-found VB love over on his blog (and look for some posts about the fact that his team is hiring!)

Nullables in VB – must read article

There is a lot of potential for using nullables incorrectly in VS2008 in Visual Basic without even realizing there’s a problem, or encountering a problem that makes no sense.

Check Bill McCarthy’s article in the October issue of visual Studio Magazing (not online yet but watch this space) is really helpful for pointing out gotchas.

I had a conversation with Bill recently where i was asking some of these questions and he was happy to be able to say “actually, I just wrote an article about that and it will be out soon.”

Ahh, serendipity.

Three great MSDN LINQ resources for VB developers

While there are many great LINQ resources for VB, I happened to read/watch/see these and wanted to be sure to point them out.

1) Lisa Feigenbaum’s LINQ Best Practices webcast. Lisa is on the VB team. This webcast is full of great insights and tips.

2) Timothy Ng’s Basic Instincts: Lambda Expressions article in the current (Sept 07) issue of MSDN Magazine. I’ve spent so much time trying to interpret lambda articles written in C#. Having one written specifically for VB devs is awesome and this article gently leads you into the complexities in a very easy to comprehend way.

3) 101 LINQ Samples for VB (yay!) was posted a few weeks ago. LINQ to Objects, to SQL, to XML and to DataSets. Don’t go looking for LINQ to Entities yet though.

 

Re-Visiting an old VB6 problem – the disappearing mouse wheel-scrolling functionality

A million years ago, when Windows 2000 came out, many VB6 developers got calls from users that the scroll wheel stopped working in their VB6 applications.

It didn’t take long for the fix (Install Intellimouse v4.0) to get shared on newsgroups and forums. (This was pre-blog days; do you even remember those?)

I have a client who has that Intellimouse install tucked away on the server to apply to new computers being set up with my old apps and it’s worked charmingly for years.

But a few months ago, a user got a new p.c. and the fix didn’t work. His job requires him to make heavy use of a function that has a big FlexGrid and not having the ability to use the scroll wheel definitely cramped his style and impeded the efficiency of his work.

I found a KB that offered a solution for the IDE, but not compiled exes. I dug into the mouse drivers on his compuer but there was no way to uninstall them.

Finally, on the VB newsgroups, MVP Ken Halter suggested a code fix that was on PlanetSourceCode.

Scroll Wheel Support: http://www.planetsourcecode.com/vb/scripts/ShowCode.asp?txtCodeId=48722&lngWId=1

It contains two modules that futz around with some Win32 stuff to get the scroll wheel to be noticed by VB6. You need to add those modules to your project and then make a call to a function passing in the window handler of your control when you fire up your form and then a detach function as you unload the form.

Simple as heck and worked like a charm!

(and generated this very happy note from the user: IT WORKS!!!  THANKS!!)

Careful with those lambdas, sonny

Well, sonny is me, actually.

I spent quite a long time trying to understand why my LINQ average function wasn’t working.

Paring down to a simple example, I wanted to check the average of a filtered subset of data.

Say my data is an array if integers:

int[] nums = { 84, 123, 101, 94, 238 };

I can get the average of these by calling:

var numavg=nums.Average();  –returns 128.0

I then wanted to get the average of only those numbers that were greater than 100.

But wait, I really started this in VB! So I was writing this function:

Dim avgnums = mynums.Average(Function(n) n > 100)

Which compiled and ran, but returned -0.333333333333.

I looked at it inside and out, over and under and could not figure out what was going on.

so then I tried it in C# and got this compiler error:

Cannot convert lambda expression to delegate type ‘System.Func<int,int>’ because some of the return types in the block are not implicitly convertible to the delegate return type 

Finally I noticed the other error listed in the Errors window:

Cannot implicitly convert type ‘bool’ to ‘int’

And realized the error of my ways.

I was thinking that my lambda was for filtering, but all I was doing was an eval which was returning trues and falses depending on whether the value was >100 or not. And the average function was the same as Average(0,0,0,0,-1,-1) which is -2/6. So -0.333333333 was correct!

[note – Option Strict was off which imacted this – see additional comments at the end of this post]

So what is the correct way to do what I wanted? I need a filtering method, duh!

in C#:

var numavg3 = nums.Where(q => q > 100).Average();

in VB

Dim avgnums2 = mynums.Where(Function(n) n > 100).Average(Function(n) n)

About VB’s Option Strict in this example

As Bill McCarthy points out, Option Strict=On would have caught the attempt to use a boolean in the Average function, forcing me to specifiy the types in the array, which I *had* in fact done in the C# code, (because it forced me to). Option Strict is one of those things you need to change to “ON” after you install Visual Studio. I had missed it in this installation and because I was focused on what I was doing wrong with the lambdas, it hadn’t even occurred to me.

Dim mynums() As Integer = {10, 20, 40, 100, 120, 140}

Otherwise, he compiler tells you: “Option Strict on requires all variable declarations to have an ‘As’ clause.” Which is a lie. There are cases where it is required and cases where it is not. Note my VB code for calculating the average is still valid and avgnums2 is recognized as a Double at compile time.

On the other hand, VB’s implicit casting is still in play as is demonstrated by then taking that double and concatenating it with a string.

 Dim x = avgnums2 & “xya”

Implicitly Typed Local Variables in VB9

At first glance, the new ImpliciltyTyped Local Variables in C#3.0 and VB9 might look familiar to VB programmers.

Earlier (that would be “current” as well) versions of VB can do a lot of implicit stuff. This has been a point of aggravation to many non-VB programmers, but I guess they have a new perspective. Next thing you know they’ll be using declarative languages! Eek!

In VB2005, we can create an integer implicitly and do integer-like things with it.

  Dim ii = 5
  Console.WriteLine(ii + 2)

Or we can do the same with something more complex like a FileInfo class. I’m not explicitly declaring files, but VB infers it from what is returned by New DirectoryInfo.GetFiles (an array of FileInfo objects).

   Dim files = New IO.DirectoryInfo(“C:\”).GetFiles

We can even go a bit further with an iteration, using f to represent each FileInfo object without explicitly defining it.

   For Each f In files
       Console.WriteLine(f.FullName)
   Next

All of this runs just fine.

But, if you were actually typing this code, you would notice that the compiler does not comprehend the types at design time.

That’s because it can’t really identify the type at design time. All that’s happening here is that we are getting late binding.

The undeclared f (FileInfo) doesn’t even give you this much help with late binding. No method or property suggestions and it’s just an object.

So here is where VB9 is definitely different. We are truly getting strong typing, not just the intelligence of late binding.

When using the implicit variables in VB9, the compiler is smarter and much more helpful!

In the For Each, f is now recognized as a FileInfo and gives intellisense as well.

Note that all of this is using the default VS2008 properties for Compiler Options. Jim Wooley has an interesting blog post about Option Infer (a new option) which allows the strong typing to occur.