嗔秤戻幣�哉膵�云利匈嬉蝕湊蛸賜�塋床四衲���萩晦編報炎嘔囚^泡仟 ̄云利匈�《超噌�殻窟�嵌虜隆輓麈觚翹瀘卉韮�仍仍�。� 烏御危列
浪慕利 卦指云慕朕村 厘議慕尺 厘議慕禰 TXT畠云和墮 序秘慕杏 紗秘慕禰

VB2008貫秘壇欺娼宥(PDF鯉塀哂猟井)-及16嫗

梓囚徒貧圭�鮗� ○ 賜 ★ 辛酔堀貧和鍬匈��梓囚徒貧議 Enter 囚辛指欺云慕朕村匈��梓囚徒貧圭�鮗� ● 辛指欺云匈競何��
!!!!隆堋響頼��紗秘慕禰厮宴和肝写偬堋響��




that does something is like a folder with stuff in it。 When you are creating the filing system�察�you  

don¨t care about the contents of the folder。 And when you fill the folder�察�you generally don¨t  

care about the filing system。 

     Modules�察�classes�察�namespaces�察�and methods are all concepts used to organize source  

code。 A method is filled with source code and does something like add numbers or create a  

textual string。 

     One of the most mon things that you will do when filling a method with source code is  

reference other pieces of organized source code。 Think of referencing as putting a sticky note  

in a folder with the text�察 �Please also look in folder B。 ̄  

     Following is a piece of source code that is 100�ァ�organizational and does nothing。 



Namespace MyMainTypes  

   Public Class AType  

       Public Shared Sub DoSomething�┌� 

       End Sub 

    End Class 

End Namespace 



Module AnotherType  

    Public Function DoSomething�┌� As Integer 

    End Function 

End Module 



     The source code has three levels of organization。 A namespace ��MyMainTypes in the example��  

encapsulates types like classes ��AType in the example��。 Classes and modules ��like AnotherType  

in the example�� encapsulate methods ��DoSomething�┌� in the example�� or properties。 Within a  

namespace�察�all types must be unique。 You can have two types with the same identifier in different  

namespaces。 Within a type�察�you cannot have identical identifiers with identical parameters。  

��This will be clearer as you learn more about Visual Basic in the uping chapters。�� 

     Visual Basic has the additional rule that classes and modules ��like AType and AnotherType��  

are in the namespace of the Visual Basic project。 That namespace is not explicitly defined�察�but  

it is identified by the project�察�as shown in Figure 2´6。 

     In Figure 2´6�察�the Root Namespace text box shows the root namespace of all types in a  

Visual Basic project。 So if both AType and AnotherType were part of the project�察�the full´length  

identifiers would be Calculator。MyMainTypes。AType and Calculator。AnotherType。 


´´´´´´´´´´´´´´´´´´´´´´Page 54´´´´´´´´´´´´´´´´´´´´´´´

32        CH AP T E R   2   *    L E A R N IN G   AB OU T   。 N E T  N U M B E R   A N D   V A L U E   T Y P E S  



          Figure 2´6。 Default root namespace of project 



               Following is the same organizational code with some source code added to do something  

          ��shown in boldface��。 



          Namespace MyMainTypes  

             Public Class AType  

                 Public Shared Sub DoSomething�┌� 

                 End Sub 

              End Class 

          End Namespace 



          Module AnotherType  

              Public Function DoSomething�┌� as Integer 

                  MyMainTypes。AType。DoSomething�┌� 

              End Function 

          End Module 



               In the bolded code�察�there is a reference to another namespace�察�type�察�and method with a  

          pair of parentheses。 This makes a method call on a shared class and shared method。 It says that  

          the implementation of the method is the calling of another method。 


´´´´´´´´´´´´´´´´´´´´´´Page 55´´´´´´´´´´´´´´´´´´´´´´´

                                   CH A PT E R   2   *    L E A R N I N G   A B OU T   。 N E T  N U M B E R   AN D   V A L U E   T Y P E S  33 



     Notice how the other method is referenced using both namespace and type identifiers。 Also  

notice how there is no reference to the Calculator namespace。 This is not necessary because�察 �

from the perspective of AnotherType�察�Calculator is implied。 This is how all types and methods  

are always referenced。 A namespace identifier is necessary only if the type ��for example�察�class��  

is not defined in the current namespace。 

     If you have namespaces with long names�察�this referencing can get tedious。 As an alterna

tive�察�you can add an Imports statement to reference the namespace�察�similar to the following。 



Imports Calculator。MyMainTypes 

Module AnotherType  

    Public Function DoSomething�┌� as Integer 

        AType。DoSomething�┌� 

    End Function 

End Module 



     The Imports statement says that if the code references any types that are not defined locally�察 �

look in this namespace ��Calculator。MyMainTypes in the example�� to find the type。 When using  

the Imports statement�察�you need to specify the full namespace of Calculator。MyMainTypes。  

Note that if you use two namespaces that have identically named types�察�you will get a piler  

failure�察�because the piler won¨t know which type to reference。 

     This covers the absolute basics of writing some code�察�and we are ready to use Visual Basic  

to do something。 



Writing the Add�┌� Method 



We¨ll write the code to add two numbers。 To begin�察�create a new project in Visual Basic�此�



     1。  Open Visual Basic Express。 ��If Visual Basic Express is open�察�choose File  Close Project  

         to ensure you have a clean slate。�� 



     2。  Click File  New Project or choose Create�此�Project from the Start Page tab。 



     3。  Choose Class Library�察�name it Calculator�察�and click OK。 



     4。  Rename Class1。vb to Operations。vb。 



     5。  Save the solution。 



     We can now write the Add�┌� method。 Add the bolded code to the Operations。vb file。 



Public Class Operations 

    Public Shared Function Add��ByVal number1 As Integer�察�ByVal number2 As _ 

Integer�� As Integer 

        Return number1 �� number2 

    End Function 

End Class 



     This simple code actually has many different pieces that fit together。 The type Operations  

is implied to be in the namespace Calculator because the default root namespace is the same  

as the project。 Defined within Operations is a method that is both Public and Shared。 Using the  


´´´´´´´´´´´´´´´´´´´´´´Page 56´´´´´´´´´´´´´´´´´´´´´´´

34         CH AP T E R   2   *    L E A R N IN G   AB OU T   。 N E T  N U M B E R   A N D   V A L U E   T Y P E S  



           Function declaration implies that the caller expects to get a value back from the method。 The  

           data type of the returned value is specified by the As Integer keywords!this particular func

           tion will return an Integer value。 ��If you want to define a method that does not return a value�察 �

           use Sub rather than  Function。�� 

                 Methods and parameters must be associated with a type�察�as Visual Basic is a type´safe  

           programming language。  Type´safe means that when you write code�察�you know what you are  

           manipulating。 

                 Suppose that you are writing code and are confronted with the numbers 1�察�1。0�察�and ;1。0;。  

           To you�察�these three numbers are identical。 But in the context of the source code�察�they are not  

           identical。 The 1 is an integer�察�the  1。0 is a double�察�and the  ;1。0; is a string。 When you want to  

           add�察�subtract�察�or otherwise manipulate pieces of data�察�they should be the same types�察�other

           wise�察�you might run into consistency errors。 Type´safe programming languages help avoid  

           such problems。 The  number types are discussed in more detail in the ^Understanding the  

           CLR Numeric Types ̄ section later in this chapter。 

                 The declaration of Add�┌� says that we need to pass in two integer´based numeric values�察 �

           and the method returns an integer´based numeric value。 The bination of parameters and  

           a return type is a method signature。 The method signature bees important when another  

           piece of code calls the Add�┌� method。 The other piece of code must use the same types as the  

           declaration。 Figure 2´7 shows a piece of code that calls the Add�┌� method�察�which we¨ll do from  

           another application in the next section。 



                                                                     Namespace           Class            
卦指朕村 貧匯匈 和匯匈 指欺競何 壘��0�� 家��0��
隆堋響頼��紗秘慕禰厮宴和肝写偬堋響��
梁椣戻幣�� 梁心弌傍議揖扮窟燕得胎��傍竃徭失議心隈才凪万弌誌育断蛍�輌臆惨軼僑〃�燕慕得珊辛參資誼持蛍才将刮襲潜��範寔亟圻幹慕得 瓜寡追葎娼得辛參資誼寄楚署衛、持蛍才将刮襲潜填��