Local Type Inference di VB 9.0

Posted at : Oct/09/2007
4265 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)

Email (required-will not published)

Comment
irtu
Input code above below (Case Sensitive) :

ABOUT ME

Rully Yulian MF
Rully Yulian Muhammad Firmansyah | Founder & IT Trainer Native Enterprise | MCT (2008-2019) | MVP (2009-2016) | Xamarin Certified Professional | MTA | MCAD | MCPD | MOS | Bandung, West Java, Indonesia.

[Read More...]

TOP DOWNLOAD

Mapping Hak Akses User Pada MenuStrip Sampai Control Button
downloaded 6983 times

Bagaimana caranya menginstal database ketika deploying sebuah aplikasi?
downloaded 4893 times

Simple Voice Engine Application With Sound Player Class...
downloaded 4045 times

Change Group,Sort Order, Filtering By Date in Crystal Reports
downloaded 3460 times

WinForms DataGrid Paging With SqlDataAdapter
downloaded 2881 times


LINKS

CERTIFICATIONS

Xamarin Certified
MOS 2007
MCT
MCPD
MCTS
MCAD.NET
ASP.NET Brainbench

NATIVE ENTERPRISE

Native Enterprise - IT Training

FOLLOW ME

Youtube  Facebook  Instagram  LinkedIn   Twitter

RSS


NATIVE ENTERPRISE NEWS

© Copyright 2006 - 2024   Rully Yulian MF   All rights reserved.