Local Type Inference di VB 9.0

Created at : Oct/09/2007  
1307 Views   2 Comments

Local Type Inference merupakan fitur baru yang terdapat di VB 9.0. Fitur baru ini memungkinkan programmer untuk mendeklarasikan variabel tanpa harus mendefinisikan tipe datanya. Lho bukannya fitur ini sudah ada semenjak VB versi non .NET?? Di VB 6.0 kita juga bisa deklarasikan variabel tanpa harus secara eksplisit mendefinisikan tipe datanya. Namun di VB 9.0 ini ada perbedaan dengan teknik yang biasa dilakukan di VB 6.0.

Lalu apa perbedaanya? Kalau di VB 6.0 kita deklarasikan variabel tanpa mendefinisikan tipe datanya maka variabel tersebut memiliki tipe data Variant. Tipe data ini sama dengan tipe data Object di VB.NET. Tipe data variant dapat menampung jenis tipe data apa saja, begitu juga dengan tipe data Object. Akan tetapi kalau Local Type Inference ini dilakukan di VB 9.0 maka variabel tersebut tidak menggunakan tipe data Object yang tidak strongly typed.

Lalu bagaimana hal tersebut dapat terjadi? Ketika code yang mengandung Local Type Inference itu di compile, maka compiler akan mengecek value yang diberikan kepada variabel tersebut. Compiler akan mengetahui sendiri tipe data yang digunakan untuk variabel tersebut. Dan hasilnya strongly typed. Hal tersebut dapat kita buktikan dengan membuka IL yang digenerate oleh compiler tersebut. Kita dapat melihatnya dengan ILDASM.exe.

Ok...misalkan saya punya beberapa variabel local yang menggunakan fitur Local Type Inference seperti dibawah ini :

   1:  Imports System.Windows.Forms
   2:   
   3:  Module NewInVB90
   4:      Private Sub MyLocalTypeInference()
   5:          Dim i = 10
   6:          Dim s = "VB 9.0"
   7:          Dim d = 2.56
   8:          Dim c = "R"c
   9:          Dim b = True
  10:          Dim t = #10/9/2007#
  11:   
  12:          MessageBox.Show(i.GetType.ToString, "Local Type Inference")
  13:          MessageBox.Show(s.GetType.ToString, "Local Type Inference")
  14:          MessageBox.Show(d.GetType.ToString, "Local Type Inference")
  15:          MessageBox.Show(c.GetType.ToString, "Local Type Inference")
  16:          MessageBox.Show(b.GetType.ToString, "Local Type Inference")
  17:          MessageBox.Show(t.GetType.ToString, "Local Type Inference")
  18:      End Sub
  19:   
  20:      Sub Main()
  21:          MyLocalTypeInference()
  22:      End Sub
  23:  End Module

Hasil messagebox yang pertama:

Hasil messagebox yang kedua :

dan untuk hasil messagebox lainnya yaitu bersesuaian dengan value yang diberikan. Sekarang kita buka IL code nya dengan ILDASM.exe via .net fx command prompt.

Sekarang kita dapat lihat bahwa di IL code semua Local Type Inference akan dirubah tipe datanya sesuai dengan tipe data berdasarkan value yang diberikan kepada variabel tsb. Jadi hasilnya bukan Object data type, but it's strongly typed :)


[Comments]
Michael Rawi Dec/31/2007 04:25:53 :
Nice info. However, this new feature are double headed sword to implement. The good one is only to save typing time, while the bad reason is that feature can cause a pain code management problem in the feature, especially when you have team of developers. For me, I strongly didn't recommend to use this feature at all... Regards Michael Rawi
Rully Dec/31/2007 04:55:04 :
Hello Mr.Rawi...thanks for the comment...this feature only works in local procedure scope, it didn't work for global variable or class level variable...IMO it couldn't cause code management problem...this feature can helps a lot..maybe if we're working on LINQ or XML Literal..or if you're enumerating the collection with its counter.

[Write your comment]
Name (required)
URL (optional)
Example : http://www.yulianmf.com  
Comment

OJSF
Input code above below (Case Sensitif) :
About Me 
Rully Yulian MF
My Name is Rully Yulian Muhammad Firmansyah. I am an IT Trainer, IT Consultant and Application Developer spesializing in Microsoft .NET technology and SQL Server database. I live in Bandung, Indonesia. My hobby is to play Guitar. [Read More...]
Top Download 
Bagaimana caranya menginstal database ketika deploying sebuah aplikasi? : Downloaded 2231 times  
Change Group,Sort Order, Filtering By Date in Crystal Reports : Downloaded 2192 times  
Simple Voice Engine Application With Sound Player Class... : Downloaded 1522 times  
DataGridView Grouping : Downloaded 1243 times  
WinForms DataGrid Paging With SqlDataAdapter : Downloaded 1232 times  
Article Category 
Links 
Award 
Certifications 
MOS 2007
MCAS
MCT
MCPD
MCTS
MCAD.NET
ASP.NET Brainbench
Native Enterprise - IT Training 
Native Enterprise Facebook Group 
My Facebook 
My LinkedIn 
Syndication (RSS 2.0) 
Powered By 
Native Enterprise News