Visual Basic 2008 9.0 .NET Examples and Ebook

New in Visual Basic 2008 - 9.0

Introduction to Visual Basic

Volgend Onderwerp

Partial Methods

Local Type Inference

Volgend Onderwerp
-


Methods can be defined as Partial. The implementation of these methods can then be completed somewhere else within the same classdefinition.

Only Private Subs without implementation can be defined as Partial.


Class SomeClass
    Partial Private Sub somePartialMethod1()
    End Sub
    Partial Private Sub somePartialMethod2()
    End Sub
    '
    Private Sub somePartialMethod1()                                       ' (1)
        Console.WriteLine("somePartialMethod1")
    End Sub
    '
    Public Sub TestSomePartialMethod1()
        somePartialMethod1()
    End Sub
    Public Sub TestSomePartialMethod2()
        somePartialMethod2()
    End Sub
End Class
Partial Class SomeClass
    Private Sub somePartialMethod2()                                       ' (2)
        Console.WriteLine("somePartialMethod2")
    End Sub
End Class
Class Example1
    Public Shared Sub Main()
        Dim someObject As New SomeClass
        someObject.TestSomePartialMethod1()
        someObject.TestSomePartialMethod2()
        '
        Console.ReadLine()
    End Sub
End Class
Download Broncode

Output :

 somePartialMethod1
 somePartialMethod2

One can complete the Partial method within the same partial classdefinition as it is defined (1). But more frequently Partial methods are completed in an other partial classdefinition (2).

Partial constructs, for instance classes and methods, are often used to hide ( and avoid changes ) to generated code.

Suppose next class Stock was generated by a code-generator. When the system, that generated this code, wants to provide the possibility to define what needs to happen when the Price property is set, Partial methods onPriceChanging and onPriceChanged could be provided.


#Region "Some Generated Code"
Partial Class Stock
    Private m_Price As Decimal
    Public Property Price() As Decimal
        Get
            Return m_Price
        End Get
        Set(ByVal value As Decimal)
            onPriceChanging(value)
            m_Price = value
            onPriceChanged()
        End Set
    End Property
    Partial Private Sub onPriceChanging(ByVal value As Decimal)
    End Sub
    Partial Private Sub onPriceChanged()
    End Sub
End Class
#End Region
Download Broncode

The programmer can now define what needs to happen when the price is changing or is changed, without the need of changing the generated setter for Price.
The risk of introducing an unstable implementation for this setter, for instance without implemenation m_Price = value, is avoided.


Partial Class Stock
    Private Sub onPriceChanging(ByVal value As Decimal)
        Console.WriteLine("Doing something when the price is changing to " & _
                          value & ".")
    End Sub
    Private Sub onPriceChanged()
        Console.WriteLine("Doing something when the price changed.")
    End Sub
End Class
Class Example3
    Public Shared Sub Main()
        Dim stock1 As New Stock
        stock1.Price = 10
        '
        Console.ReadLine()
    End Sub
End Class
Download Broncode

Output :

 Doing something when the price is changing to 10.
 Doing something when the price changed.

A performance overhead is avoided by the compiler by ignoring calls to Partial methods, when no completion of the methods are found.


Partial Class SomeClass
    Partial Private Sub somePartialMethod3()
    End Sub
    '
    Public Sub TestSomePartialMethod3()
        somePartialMethod3()                                               ' (1)
    End Sub
End Class
Class Example2
    Public Shared Sub Main()
        Dim someObject As New SomeClass
        someObject.TestSomePartialMethod3()
        '
        Console.ReadLine()
    End Sub
End Class
Download Broncode

Call somePartialMethod3 (1) will be ignored by the compiler.





This version ( published on 2008-06-24 ) is printed from http://www.studyvb.com, visit the website for more recent information.

Updated On : 2008-04-07

Download Broncode

Published On : 2008-06-24

Partial Methods

Local Type Inference

Volgend Onderwerp

New in Visual Basic 2008 - 9.0

Introduction to Visual Basic

Volgend Onderwerp
Nederlands  Nederlands

Add to favorites (IE).