Hi guys. Lately, I've been into a number of Late Bound, Dynamically Typed Languages. But, the .NET Framework itself ships with
two Dynamic Languages. One is easy to spot, in the form of JScript.NET. But, the other one is much more mainstream and harder to spot: It's VB.NET. Yes, VB.NET can be turned into a Dynamic Language with just TWO Lines of Code. Well, I know many VBers know this, but for those who don't here are those 2 lines:
Option Strict Off
Option Explicit Off
Well, I know turning them
on is considered a good practice, and they are on by default. But, setting them to off lets you do some pretty amazing things, like :
d= 100
console.writeline(d.GetType) 'Prints System.int32
d = 123123123123123
console.writeline(d.GetType) 'Prints System.int64
d = "Hello Guys!"
console.writeline(d.GetType) 'Prints System.String
d = new System.Diagnostics.Process
d.Start("cmd.exe") 'This ACTUALLY starts a new a new Command Window
Console.Writeline(d.GetType) 'Prints System.Diagnostics.Process
So, the type actually changes every time you assign something to it! Well, that's not amazing, since you can just do the same in C#. But, it's elegant, without casts[
which I absolutely hate]. So, this is called
Late Binding
.
And, variable d is
not declared before. Well, what's the advantage of this ? Well, nothing. Although some people will say that Boxing and Unboxing along with Late Binding will slow down performance, I don't think that will be very noticable unless you're doing some very deep recursion or nesting.
Only catch is, as of now, VS won't offer you complete Intellisense, and Late Binding uses Reflection, which is quite a bit slower. But, VB9 actually goes a really long way into solving these problems. Have a look at the
VB Future section at MSDN for some more cools tuff about VB9.
And, what is the
use of it ? Well, with this, VB.NET is the
most powerful Scripting Language I Know. How ? Well, the .NET Framework is all encompassing, and I can always call into the Win32 API if I want something not there. And, mostly, whatever I want to do, .NET offers it. So, why learn something new, when I can use a powerful language as my Scripting Language ? And, that too, a language I love:D
So, here's a small script I wrote, using
my previous snippet to get thumbnails of images, to convert a folder of photos into 800x600 size, enough to reduce size without compromizing quality:
Dim s As String = "C:\School Pics\100OLYMP\Edited"
d = New IO.DirectoryInfo(s)
i =Drawing.Image
Console.writeline (d.FullName )
console.WriteLine(d.GetFiles.Length)
Console.ReadKey
For Each f In d.GetFiles
If f.Extension.ToUpper.Contains("JPG") Then
Console.WriteLine ("Processing : " & f.FullName)
i = drawing.Image.FromFile(f.FullName)
i.GetThumbnailImage(800,600, Nothing, System.IntPtr.Zero).save _
(d.FullName & "\Thumb" & f.Name & ".jpeg", _
drawing.Imaging.ImageFormat.Jpeg)
End If
Next
console.ReadKey
Prefix that with a Module and Sub Declaration, and you've got a little neat script that runs fast, and converts everything in the folder I specified to my Size of 800x600. Well, try doing that in, huh,
Batch Files, and see the pain....:D
Well, I'm investigating how latebound calls are translated to IL, and I'll post something about it very soon....
Till then, keep coming, and keep commenting:D
And, thanks for reading this rather long article...