Tuesday, October 2, 2007

Bindable List solved and IIF

So my investigation found that the ComponentModel namespace contains classes and such to work with components. Not very helpful in answering my question unless you know that components is another name for controls. So this namespace contains classes and such for working with controls, including binding them to data. So I guess it does make sense that the BindingList(Of T) is placed in this namespace. I'm still not sure that I like the idea that there is no reference to this type in the System.Collections.Generic namespace. I know that would have saved me some time and head scratching. Oh well, at least I know it's there now. Just 15 minutes of my life I will never get back.

On another note, after getting a null reference exception when using the IIF() function in vb, some searching led me to find that since the IIF function is a built in function and not a operator, it's arguments get evaluated before they are sent to the function. That explains my exception. Here's what I was trying to do:

Dim sb As New System.Text.StringBuilder
sb.AppendFormat("Name: {0}", IIf(object Is Nothing, String.Empty, object.name))

Basically I was trying to do an inline test to prevent a null reference. Unfortunately it didn't work. Here's what I ended up with:

sb.AppendFormat("Name: {0}", getName(object))

...
Private Function getName(ByVal o As Object) As String
    If o Is Nothing Then
        Return String.empty
    Else
        Return o.name
    End If
End Function

Or at least that's the dumbed down version. My actual situation was slightly more complex. Apparently, the functionality of the IIF statement is something that the VB team was looking at in VB9. I haven't been able to find any reference to any actual implementations in the new version yet, but I hope they did something. With each version VB is continually growing and becoming a stronger language. I agree with some of the detractors that earlier versions of VB were not as powerful as other languages, but I think now it is quickly gaining ground. Personally, I have been using vb in different forms (VBA, VB6, and now VB.Net 2005) for several years and love the fact that I can use most of the same syntax whether I am writing an Excel or Access macro, a full blown .Net application, or an ASP.Net web application. I know you can use C# for web apps and I believe you might even be able to use it with the newer versions of Office. I still like the readability of VB. Why do I have the tell the compiler where my lines of code end? Why would I want to add extra characters to enclose code blocks?

One last gripe. While reading a post on the IIF problem, there were several comments on the post about how C# is better or that vb is an inferior language. I just wish that every post that even comes close to pointing out a problem with VB didn't end up as an argument over which language is better. Each language has it's own features and bugs. Just because you can do one thing better in a different language doesn't make it 'better'. If you like it, use it. If you don't, use yours and let us use ours without sticking your nose in. If you hate VB or think its inferior, why are you even reading a post about VB? We all have our reasons for choosing the tools we use. If VB ever becomes more of a hindrance than a help for what I need, I'll be the first to switch. But for now it serves my purpose better than any other, and I have looked. Just like any debate on religion or the Ford vs. Chevy battle, its all a matter of opinion.

EDIT: After posting I found out that they did add to the language to fix the IIF problem in VB9.

No comments: