First Look Of Some New Beta 1 VB 10 Language Features (Part-1)

Posted at : May/21/2009
3567 Views | 0 Comments

Pada artikel kali ini saya akan membahas beberapa fitur baru dari sisi language untuk bahasa VB 10 Beta 1. Adapun beberapa language fitur yang akan dibahas disini yaitu : Array Literals, Collection Initializer, Implicit Line Continuation, Auto Implemented Properties and Property Value Initialization, Multiline Lambda Function and Sub Lambdas.

Penjelasan dari fitur-fitur baru tersebut dituliskan didalam code comment.

1. Implicit Line Continuation

   1: '//Penggunaan underscore di VB digunakan untuk memecah
   2: '//kode program menjadi beberapa baris
   3: '//Pada VB 10 penggunaan underscore ini dihilangkan
   4: '//untuk kondisi-kondisi tertentu, fitur ini disebut dengan
   5: '//Implicit Line Continuation
   6: '//kondisi dimana fitur ini dapat digunakan yaitu :
   7: 
   8: '1.    Setelah attribute
   9: '2.    Setelah koma
  10: '3.    Setelah titik
  11: '4.    Setelah binary operator
  12: '5.    Setelah LINQ query clause
  13: '6.    Setelah a (, {, or <%=
  14: '7.    Sebelum a ), }, or %>
  15: 
  16: '//Implicit Line Continuation setelah penggunaan Attribut
  17: <System.ComponentModel.Browsable(True)>
  18:     <System.ComponentModel.Category("Roel's Category")>
  19:     Public Property GetColor As System.Drawing.Color = Color.Azure
  20: 
  21: '//Implicit Line Continuation setelah penggunaan koma, (, dan )
  22: Private Function FuncWithImplicitLineContinuation(
  23:                                                  ByVal teks As String,
  24:                                                  ByVal title As String
  25:                                                  ) As DialogResult
  26: 
  27:     Return MessageBox.Show(teks, title, MessageBoxButtons.YesNo,
  28:                            MessageBoxIcon.Question
  29:                            )
  30: 
  31: End Function
  32: 
  33: Private Sub AnotherImplicitLineContinuation()
  34: 
  35:     '//Implicit Line Continuation setelah LINQ query clause, {, dan }
  36:     Dim bilGenap =
  37:         From num In
  38:         {
  39:             1,
  40:             2,
  41:             3
  42:         }
  43:         Where num Mod 2 = 0
  44:         Select num
  45: 
  46: 
  47:     '//Implicit Line Continuation setelah <%=, %>, {, dan }
  48:     Dim bookList As List(Of Book) = New List(Of Book) From
  49:         {
  50:             {1, "Visual Basic 10"},
  51:             {2, "Visual C# 4.0"}
  52:         }
  53: 
  54: 
  55:     Dim itsEasyToCreateXMLFileUsingXMLLiterals =
  56:         <Books>
  57:             <%=
  58:                 From book In bookList
  59:                 Select <Book ISBN=<%=
  60:                                       book.ISBN
  61:                                   %>>
  62:                            <Title><%=
  63:                                       book.Title
  64:                                   %></Title>
  65:                        </Book>
  66:             %>
  67:         </Books>
  68: 
  69: End Sub

2. Array Literals

   1: Private Sub btnArrayLiterals_Click() _
   2:     Handles btnArrayLiterals.Click
   3: 
   4:     '//deklarasi array di vb 9.0 (Option Infer On) :
   5:     Dim arr1() = New Integer() {1, 2, 3}
   6:     Dim arr2(,) = New Integer(,) {{1, 2}, {3, 4}}
   7: 
   8:     '//deklarasi array dengan menggunakan 
   9:     '//array literals di vb 10 (Option Infer On) :
  10:     Dim arr3 = {1, 2, 3}
  11:     Dim arr4 = {{1, 2}, {3, 4}}
  12: 
  13: End Sub

3. Auto Implemented Properties and Property Value Initialization

   1: Public Class CustomerInPreviousVB
   2: 
   3:     Private mID As Integer
   4:     Public Property IdNumber As Integer
   5:         Get
   6:             Return mID
   7:         End Get
   8:         Set(ByVal value As Integer)
   9:             mID = value
  10:         End Set
  11:     End Property
  12: 
  13: End Class
  14: 
  15: Public Class CustomerInVB10
  16: 
  17:     '//auto implemented property di vb 10
  18:     '//tidak dapat menggunakan input parameter
  19:     '//dan juga harus bersifat read dan write
  20:     Public Property IdNumber As Integer
  21: 
  22:     '//auto implemented properties
  23:     '//dapat diberi nilai default
  24:     '//ketika diinisialisasi
  25:     Public Property GetRandomNumber As Integer =
  26:         New Random().Next(1, 10)
  27: 
  28: End Class
  29: 
  30: Public Class GetOrder
  31:     Public Property ProductName As String
  32:     Public Property Quantity As Integer
  33: End Class
  34: 
  35: Public Class Book
  36:     Public Property ISBN As Integer
  37:     Public Property Title As String
  38: End Class

4. Collection Initializer

Extend method Add Generic List untuk class GetOrder dan class Book di atas :

   1: Imports System.Runtime.CompilerServices
   2: 
   3: Module Module1
   4: 
   5:     <Extension()>
   6:         Sub Add(ByVal list As List(Of GetOrder),
   7:             ByVal ProdName As String,
   8:             ByVal Qty As Integer)
   9: 
  10:         list.Add(New GetOrder With {.ProductName = ProdName,
  11:                  .Quantity = Qty})
  12:     End Sub
  13: 
  14:     <Extension()>
  15:     Sub Add(ByVal list As List(Of Book),
  16:         ByVal isbnNumber As Integer,
  17:         ByVal bookTitle As String)
  18: 
  19:         list.Add(New Book With {.ISBN = isbnNumber,
  20:                  .Title = bookTitle})
  21:     End Sub
  22: 
  23: End Module
 
   1: Private Sub btnCollInit_Click() _
   2:     Handles btnCollInit.Click
   3: 
   4:     '//collection initializer memungkinkan untuk memberikan
   5:     '//nilai default value object collection dengan
   6:     '//satu atau single expression
   7:     Dim intList = New List(Of Integer) From {1, 2, 3}
   8: 
   9:     Dim bookList = New Dictionary(Of Integer, String) From
  10:         {
  11:             {1, "Exploring VB 10"},
  12:             {2, "Exploring C# 4.0"}
  13:         }
  14: 
  15:     '//collection dari class dimana method Add
  16:     '//dari generic List sudah di extend di Module
  17:     Dim orderList = New List(Of GetOrder) From
  18:         {
  19:             {"Mouse", 10},
  20:             {"Keyboard", 5},
  21:             {"Infocus", 2}
  22:         }
  23: 
  24: End Sub

5. Multiline Lambda Function and Sub Lambdas

Asumsikan sebuah file Books.xml dengan struktur dibawah ini :

   1: <?xml version="1.0" encoding="utf-8" ?>
   2: <Books>
   3:   <Book ISBN="1">
   4:     <Title>Visual Basic 10</Title>
   5:   </Book>
   6:   <Book ISBN="2">
   7:     <Title>Visual C# 4.0</Title>
   8:   </Book>
   9:   <Book ISBN="3">
  10:     <Title>VSTS 2010</Title>
  11:   </Book>
  12: </Books>

 

   1: Private Sub btnMultilineLambdas_Click() _
   2:     Handles btnMultilineLambdas.Click
   3: 
   4:     '//lambda function di vb 9.0
   5:     Dim bilGenap1 = Function(num As Integer) num Mod 2 = 0
   6:     Dim isGenap1 = bilGenap1(2)
   7: 
   8:     Dim visualBooks1 = Function(xdoc As XDocument) xdoc...<Book>.Where(
   9:                       Function(elem) elem...<Title>.Value.ToLower.Contains("visual"))
  10:     '//eksekusi lambda function
  11:     Dim books1 = visualBooks1(XDocument.Load("../../Books.xml"))
  12: 
  13: 
  14:     '//multiline lambda function di vb 10
  15:     Dim bilGenap2 = Function(num As Integer)
  16:                         Return num Mod 2 = 0
  17:                     End Function
  18:     Dim isGenap2 = bilGenap2(2)
  19: 
  20:     Dim visualBooks2 = Function(xdoc As XDocument)
  21:                            Return From elem In xdoc...<Book>
  22:                                   Where elem.<Title>.Value.ToLower.Contains("visual")
  23:                                   Select elem
  24:                        End Function
  25:     '//eksekusi lambda function
  26:     Dim books2 = visualBooks2(XDocument.Load("../../Books.xml"))
  27: 
  28: End Sub
  29: 
  30: Private Sub btnSubLambdas_Click() _
  31:     Handles btnSubLambdas.Click
  32: 
  33:     '//vb 9 hanya mengijinkan untuk membuat lambda function
  34:     '//yang mengembalikan sebuah value, artinya tidak menyediakan
  35:     '//fitur untuk membuat Sub Lambda 
  36: 
  37:     '//error di vb 9.0 (doesn't produce a value)
  38:     'Array.ForEach({1, 2, 3}, Function(num) IIf(num mod 2 = 0,
  39:     '                                           Console.WriteLine("Genap"),
  40:     '                                           Console.WriteLine("Ganjil")
  41:     '                                           )
  42: 
  43:     '//vb 10 memiliki fitur baru yaitu
  44:     '//Sub Lambda
  45:     Array.ForEach(
  46:                     {1, 2, 3},
  47:                     Sub(number)
  48:                         If number Mod 2 = 0 Then
  49:                             Console.Write(number.ToString)
  50:                         End If
  51:                     End Sub
  52:                   )
  53: 
  54: End Sub
 
Happy coding with new taste of VB 10 :)…post berikutnya masih akan membahas language fitur lainnya untuk VB 10 Beta 1 ini.

[Comments]


[Write your comment]

Name (required)

Email (required-will not published)

 
Comment
oghi
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 6981 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.