梓囚徒貧圭�鮗� ○ 賜 ★ 辛酔堀貧和鍬匈��梓囚徒貧議 Enter 囚辛指欺云慕朕村匈��梓囚徒貧圭�鮗� ● 辛指欺云匈競何��
!!!!隆堋響頼��紗秘慕禰厮宴和肝写偬堋響��
#Else A default code block that is included if the other #If statements did
not trigger。
#Region and These have absolutely nothing to do with the pilation of the source
#EndRegion code。 They are used by Visual Studio to create regions of source code that
can be ^folded。 ̄ Folding means to collapse a block code out of sight
temporarily。 Source code files can be bee long�察�and constantly paging
up and down bees tedious。 By folding the code�察�you are reducing the
amount of times that you need to page up and down�察�even though the
code still exists。
The following example demonstrates how to use the preprocessor directives。
#Const ACTIVATE_1 = True
Namespace TestDefine
Class Example
#If ACTIVATE_1 Then
Private _dataMember As Integer
#ElseIf Not NO_ACTIVATE_10 Then
Private _dataMember3 As Integer
#Else
Private _defaultValue As Integer
#End If
End Class
End Namespace
In general�察�you will use preprocessor directives when you are building debug mode or
release mode code。 Debug mode means that the code is piled such that it can be debugged
and analyzed using pretty symbols。 Release mode means when the code is piled�察�the code
can still be debugged�察�but the pretty symbols are missing。
*Note Visual Basic Express does not give you the flexibility to choose debug mode or release mode for
development。 To get this feature�察�you need the full version of Visual Studio。
Think of the pretty symbols as milestones on the road。 Every time your code executes in
debug mode�察�the code is saying�察 �You are now calling this method in this source code file�察院�or
^Oops�察�that property code in the source code file is not working properly。 ̄ In release mode�察�the
pretty symbols are more like road signs saying�察 �Place X y Miles/Kilometers。 ̄ The signs help
´´´´´´´´´´´´´´´´´´´´´´Page 181´´´´´´´´´´´´´´´´´´´´´´´
CH A PT E R 6 * L E A R N I N G T HE B AS IC S O F O B J E CT OR I E N TE D P R O G R AM M IN G 159
guide you�察�but the information you get from them is very limited�察�that is�察�you don¨t know which
towns you¨re passing。
More Property Scope Details
In this chapter¨s examples�察�the Get and Set parts of the properties always had the same scope。
However�察�properties can have mixed scope�察�that is�察�the Get and Set parts can have different
scopes。 Scope splitting works only if you specify both the Get and Set parts of the property。 The
idea behind splitting the scope is to enable the implementation of the logic where classes in the
inheritance chain are allowed to assign a property�察�and classes external to the inheritance
chain are allowed only to read the property。 The following is an example where a property is
declared as Public and the assignment of the property is Protected。
Class PropertyScopeExample
Private _value As Integer
Public Property Value�┌� As Integer
Protected Set��ByVal value As Integer��
_value = value
End Set
Get
Return _value
End Get
End Property
End Class
The MustOverride Keyword
In this chapter¨s example�察�the keyword MustInherit was used to declare a class that could be
referenced�察�but not instantiated。 It is also possible to define methods that need to be imple
mented。 In Visual Basic�察�to require a derived class to implement the method�察�you use MustOverride。
The idea behind declaring methods that need to be overridden is that it allows a developer to
define an intention in a base class that can then be implemented in the subclass。
In the implementation of HotelCurrencyTrader and ActiveCurrencyTrader�察�two methods
were defined�此�ConvertTo�┌� and ConvertFrom�┌�。 A developer could e to the conclusion that
the methods are mon to both classes�察�and thus they could be defined in the base class
CurrencyTrader。 That is not a bad idea。 Let¨s revisit the CurrencyTrader class and see how these
two methods could be added to CurrencyTrader as MustOverride methods。
Public MustInherit Class CurrencyTrader
Private _exchangeRate As Double
Protected Property ExchangeRate�┌� As Double
Get
Return _exchangeRate
End Get
Set ��ByVal Value As Double��
_exchangeRate = Value
End Set
´´´´´´´´´´´´´´´´´´´´´´Page 182´´´´´´´´´´´´´´´´´´´´´´´
160 CH AP T E R 6 * L E A R N IN G T HE B AS IC S O F OB J E CT OR I E N T E D P R O G R AM M IN G
End Property
Protected Function ConvertValue��ByVal input As Double�� As Double
Return _exchangeRate * input
End Function
Protected Function ConvertValueInverse��ByVal input As Double�� As Double
Return input / _exchangeRate
End Function
Public MustOverride Function ConvertTo��ByVal value As Double�� As Double
Public MustOverride Function ConvertFrom��ByVal value As Double�� As Double
End Class
A MustOverride method is declared without an implementation。 Any class that subclasses
CurrencyTrader is required to implement ConvertTo�┌� and ConvertFrom�┌�。 In the case of
HotelCurrencyTrader and ActiveCurrencyTrader�察�this is not a problem�察�because the methods
are already implemented。 However�察�the methods need to be changed slightly�察�as follows�此�
Public Overrides Function ConvertTo��ByVal value As Double�� As Double
' 。 。 。
End Function
Public Overrides Function ConvertFrom��ByVal value As Double�� As Double
' 。 。 。
End Function
The slight change is the addition of the keyword Overrides to indicate that the ConvertTo�┌�
and ConvertFrom�┌� methods in HotelCurrencyTrader and ActiveCurrencyTrader override the
functionality in CurrencyTrader。
While you have seen all of the technical aspects�察�the bigger question is why you would do
this in the first place。 Let¨s go back to the implementation of ActiveCurrencyTrader�察�which has
no MustOverride method implementations。 To use the class and the method ConvertTo�┌��察�you
would write the following code。
Dim cls As ActiveCurrencyTrader = New ActiveCurrencyTrader��1。46�察 �USD;�察 �EUR;��
Dim converted As Double = cls。ConvertTo��100。0��
Imagine the situation where the values to convert to and from are text。 To keep things general�察 �
you would write the following code ��which takes the value from a text box in a Windows form��。
Public Function ConvertToTextField��ByVal cls As ActiveCurrencyTrader�� As Double
Return cls。ConvertTo��Double。Parse��text1。Text����
End Function
The implementation of ConvertToTextField�┌� makes one major mistake in that it assumes
that you will always be converting a currency using the ActiveCurrencyTrader implementation。
If you wanted to use the class HotelCurrencyTrader�察�you would need to implement another
method with a parameter of type HotelCurrencyTrader。