梓囚徒貧圭�鮗� ○ 賜 ★ 辛酔堀貧和鍬匈��梓囚徒貧議 Enter 囚辛指欺云慕朕村匈��梓囚徒貧圭�鮗� ● 辛指欺云匈競何��
!!!!隆堋響頼��紗秘慕禰厮宴和肝写偬堋響��
attempt to find the quickest or shortest way。 In the abstract sense�察�the search algorithm the GPS
makers apply is identical to the search algorithm that we are going to develop in this chapter。
Implementing User´Defined Types
The data structure on which the algorithm will operate is a user´defined type。 In the examples
up to this point�察�we have been using types like Double and String�察�which are CLR´provided
types。 For the example in this chapter�察�we will define our own type�察�which we will use to repre
sent a node in the tree。
Declaring Structures and Classes
A user´defined type can be implemented in one of two ways�此�as a structure or as a class。 An
example of each is shown in Figure 4´3。
A type declaration requireA type declaration requiress a a CCLRLR In Visual Basic�察�an appropriate
conconsstruct ��e。g。�察�truct ��e。g。 SStructure or tructure or CClalassss�� and�� and keyword ��e。g。�察�Class�� opens a block。
an identifier ��e。g。�察�Node��an identifier ��e。g。 Node�� To close the block�察�the keyword is
bined with the End identifier。
Everything in between belongs to
Structure Node Class Node the keyword。 So�察�if the keyword is
End Structure End Class Class�察�everything within the block
belongs to the creation of a class
Structure is used to define
a custom value type ��e。g。�察�
Double���察�which has some Class is used to define a custom reference type
restrictions and is used only ��e。g。�察�String���察�which is the most mon type
in certain situations used for development
Figure 4´3。 Choices for implementing the Node custom type
As shown in Figure 4´3�察�you can choose to create a user´defined type as a value type
��Structure�� or reference type ��Class�� type。 For the most part�察�developers use a reference type�察 �
because it has fewer constraints and is easiest to use in a general context。 A value type has some
constraints due its behavior of storing everything on the stack�察�as discussed in the following
sections。
Value Type Constraints
The constraints of using a value type relate to the fact that data is copied。 This has an impact on
what happens when you embed reference types in value types and use value types as parame
ters to methods。
´´´´´´´´´´´´´´´´´´´´´´Page 104´´´´´´´´´´´´´´´´´´´´´´´
82 CH AP T E R 4 * L E A R N IN G AB OU T D AT A S TR U CT U R E S�察 �DE CI SI ON S�察 �A N D L O OP S
The Effects of Copying Data
When one value type is assigned to another value type�察�the contents of the value types are copied。
To see this in action�察�consider the declarations in Figure 4´4。
Declaration of the identifier
Custom value type declaration value is inside the block�察�so it
belongs to the custom type
Structure MyValueType
Public value As Integer Declaration of value is associated
End Structure with a type identifier ��Integer�� and a
Class MyReferenceType scope identifier ��Public��
Public value As Integer
End Class
Custom reference type declaration
Both custom types declare a variable
that is a value type�察�which is called a
data member because it belongs to the
custom type declaration
Figure 4´4。 Declaring custom types
When declaring user´defined types�察�the data members and methods are declared inside
the block of code。 You can think of the declaration as the writing on the outside of the box�察�and
anything in the block of code as the contents of the box。 The custom types ��MyValueType and
MyReferenceType�� as declared in Figure 4´4 do not have a scope identifier。 Think of a scope identifier
as defining who has access to your pockets and wallet。 In the case of the types in the example�察 �
the scope is like saying that your spouse is allowed to peek into your wallet�察�but strangers
cannot。 In other words�察�the types cannot be used outside their own namespace。
If the Public keyword had been in front of the type identifier�察�then the user´defined type
would be exposed like a wallet that is allowed to be peeked into by the general public。 This means
the types can be used by other code。 In the case of the wallet�察�it¨s a bad idea to have unfettered
public scope�察�but sometimes public scope is desirable when you are able to control its access。
And you do this every time you pay for something by handing a credit card to the cashier。 In
that case�察�you are publicly exposing parts of your wallet under your supervision。 In code terms�察 �
this means making your types public�察�but not necessarily making the data members public。
Next�察�consider the code in Figure 4´5。 It instantiates MyValueType and assigns it to another
variable of the same type。
The example in Figure 4´5 illustrates what happens to two variables when one is instanti
ated and assigned to the other�察�and then the other is modified。 You want to understand how
each data type is modified when an interaction with another type occurs。
´´´´´´´´´´´´´´´´´´´´´´Page 105´´´´´´´´´´´´´´´´´´´´´´´
CH AP T E R 4 * L E A R N I N G A B OU T D AT A S TR U CT U R E S�察 �DE CI SI ON S�察 �A N D L O OP S 83
To create a valid instance
Variable var is declared�察 �associated with the
but a declaration does not variable var�察�you need to
Another way to associate a
imply a valid instance allocate the object using valid instance with a variable is
the New keyword
to assign the variable
��e。g。�察�copiedVar�� from another
variable that contains a valid
Dim var As MyValueType = New MyValueType�┌�
Dim copiedVar As MyValueType = var instance ��e。g。�察�var��
Console。WriteLine�─�var value=; & var。value & _
; copiedVar value=; & copiedVar。value�� When an instance is allocated�察�the data
var。value = 10 members are assigned their appropriate
Console。WriteLine�─�var value=; & var。value & _ default value�察�which is 0 for Integer and most
; copiedVar value=; & copiedVar。value��
numbers。 For strings�察�it is an empty buffer。
These lines display the empty values。
The value of var。value is
modified from 0 to 10 The contents of var and copiedVar
are displayed again to show
which variable