CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Jay Kimble -- The Dev Theologian

Philosophizing about the .Net religion

VB NEXT thoughts

I'm currently reading the overview of VB 9 (aka Orcas) here

And I just read something that made me want to stand up and applaud the VB team!  Here's the statement

"Because types of local-variable declarations are inferred by default, no matter what the setting of Option Strict is, access to such variables is always early-bound. The programmer must explicitly specify late binding in Visual Basic 9.0, by explicitly declaring variables as of type Object.. "

Finally, we are taking a step back away from this whole variant mess... Ok, let me explain.  I started with VB in version 3.  VB3 had both Object and Variant back in those days.  Variant meant that you wanted to allow any type, and object meant that you were dealing with some kind of external late bound object (usually an OLE Server like Word or Excel... for those who don't know what an OLE Server is, it's more or less the same thing as a COM object).  Variant was something you avoided like the plague because it slowed down performance. 

Along came ASP and guess what, there are only variants!  That thing you avoided in VB3 (and VB4) was now the norm.  BTW, object was still available in VB4, Vb5, and VB6, but they meant late bound (to be avoided at all costs)

A few years later, VB.Net (or VB 7) came onto the scene and variant was history (I applauded this), but object was the replacement (and like many others I was confused by this).  Many of us came to understand that the .Net Object was still a late bound way to refer to things, but it felt a little more powerful here... you weren't stuck with object; you could recast it as something else.

The real issue in .Net was the whole "Option Strict On/Off" issue.  Let's start with an example to explain this.
Option Explicit off
Public MyClass
   
Int2 = "2"
   
Int1 = 1
    Public Function GetValue() as String
      Return Int2 + Int1
    End Function
    ' More mock class methods would follow
End Class

Ok, so we have a class that has 2 class variables (not sure the above would work, since I never write code like this).  What is the type of the Str and Int1 variables?  Pencils down.  The answer is obviously Object since that is the default type.  So what happens when the someone calls the GetValue method what returns?  Honestly off the top of my head I don't know, but the problem is that the compiler will convert the variables from object to some common type... my guess is string, so you will get "21" instead of 3... or maybe you'll get 3 instead of "21."  The real issue is that the runtime has to guess what type you want to use... yuck!  Now it would be a little simpler if instead of defaulting to Object that the compiler would see the "2" and say 'Hey, "2" is a string... Int2's real type is string,' and it would see the 1 and say 'Hey, 1 is an integer' and would assign Int1's real type to Integer... now none of this really solves the GetValue thing, but soon (ok, a long time from now) we will no longer accidently use object.

BTW, another cool thing is with For.. Next loops.  I can say
For x = 0 to 10
Next

instead of

For x As Integer = 0 to 10
Next

I like that! 


Published Sep 14 2005, 07:04 PM by Jay Kimble
Filed under: ,

Check out Devlicio.us!

This Blog

Syndication

News

CodeBetter.Com Home
Current Threat level
Terror Alert Level