友情提示:如果本网页打开太慢或显示不完整,请尝试鼠标右键“刷新”本网页!阅读过程发现任何错误请告诉我们,谢谢!! 报告错误
狗狗书籍 返回本书目录 我的书架 我的书签 TXT全本下载 进入书吧 加入书签

VB2008从入门到精通(PDF格式英文版)-第15章

按键盘上方向键 ← 或 → 可快速上下翻页,按键盘上的 Enter 键可回到本书目录页,按键盘上方向键 ↑ 可回到本页顶部!
————未阅读完?加入书签已便下次继续阅读!





           Figure 1…8。  Updated solution structure that contains three projects 



                The added ClassLibrary project has a single file called Class1。vb; which is a plain…vanilla  

           source code file。  


…………………………………………………………Page 37……………………………………………………………

                                                                       C H AP TE R   1   ■    R E AD Y ;   ST E AD Y ;   G O! 15 



Moving Functionality 



Now we will move the code used to say “hello; world” from ConsoleApplication to  

ClassLibrary。 Add the code to Class1。vb as follows (the bolded code): 



Public Class Class1 

    Public Shared Sub HelloWorld() 

        Console。WriteLine(〃hello; world〃) 

    End Sub 

End Class 



     The modified code contains a method called HelloWorld()。 When called; this method will  

output the text “hello; world。” As mentioned earlier in the chapter; a method is a set of instruc

tions that carry out a task。 Methods are discussed in more detail in Chapter 2。  

     In order for applications to actually share the code that’s in a class library; you must make  

the projects aware of each other’s existence。 You do that through references。 



Defining References 



To make one project aware of definitions in another project; you need to define a reference。 The  

idea behind a reference is to indicate that a project knows about another piece of functionality。 



■Note  The project only knows about the functionality that has been declared as being public。 Public function

ality; or what Visual Basic programmers call public scope; is when you declare a type with the Public keyword。  

You will learn about public and other scopes throughout this book。  



     To make ConsoleApplication aware of the functionality in the ClassLibrary project; you  

need to set a physical reference; as follows: 



     1。  In the Solution Explorer; click ConsoleApplication。  



     2。  Right…click and select Add Reference。 



     3。  Click the Projects tab。 



     4。  Select ClassLibrary; and then click OK。 ClassLibrary will be added to  

         ConsoleApplication’s references。 



     Once the reference has been assigned; ConsoleApplication can call the functionality in  

ClassLibrary。  

     To know which references your application or class library has; you need to look in the  

project settings。 To do so; right…click the project name; ConsoleApplication; in the Solution  

Explorer and select Properties。 In the Properties window; select the References tab; as shown  

in Figure 1…9。 


…………………………………………………………Page 38……………………………………………………………

16        CH AP T E R   1   ■    R E A DY ;   ST E A DY ;   G O ! 



          Figure 1…9。 References used by the Visual Basic project 



           Calling Class Library Functionality 



          Now we need to change ConsoleApplication so that it calls the function in ClassLibrary。 Modify  

          the Module1。vb file in ConsoleApplication as follows: 



          Module Module1 



              Sub Main() 

                  Console。WriteLine(〃hello; world〃) 

                  ClassLibrary。Class1。HelloWorld() 

                  Console。ReadKey() 

              End Sub 



           End Module 



                Run ConsoleApplication by pressing Ctrl+F5。 A mand prompt window should appear  

          and generate the “hello; world” text twice。 The first “hello; world” is generated by the code  

          Console。WriteLine()。 Calling the function ClassLibrary。Class1。HelloWorld() generates the  

          second “hello; world。” 



                                             USING REFERENCE SHORTHAND 



             ClassLibrary。Class1。HelloWorld() is the longhand way to use a reference。 If we were to use long

             hand for the Console。WriteLine() call; we would write System。Console。WriteLine(); because the  

             Console。WriteLine() method is defined in the System reference。 However; Visual Basic Express includes  

             the  System reference by default; so we don’t need to do it this way。  


…………………………………………………………Page 39……………………………………………………………

                                                                         C H AP TE R   1   ■    R E AD Y ;   ST E AD Y ;   G O! 17 



   To use shorthand for the ClassLibrary call; we would include an  Imports line at the beginning of  

   Module1。vb in ConsoleApplication and change the call to Class1’s HelloWorld() method: 



   Imports ClassLibrary 

   Module Module1 



       Sub Main() 

           Console。WriteLine(〃hello; world〃) 

           Class1。HelloWorld() 

           Console。ReadKey() 

       End Sub 



   End Module 



        But shorthand like this has a downside。 What if we had many references; each containing a class called  

   Class1? In this case; Visual Basic Express wouldn’t know which Class1 to use without the help of longhand。  

   Granted; you are not likely to name multiple classes Class1; but even sensible names can be duplicated in a  

   collection of references。 And if you are using someone else’s code as a reference; the possibility of duplicate  

   names bees higher。 Therefore; you’re better off using longhand in this case。 



Using Variables and Constants 



One of the core concepts in a Visual Basic program is to use variables。 Think of a variable as a  

block of memory where you can store data for later use。 This allows you to pass data around  

within your program very easily。 

      In our ClassLibrary project; it would make life easier if we could define the message to  

display at the beginning of the method。 That way; if we decide to change the message; we can get at  

it much more easily。 As it stands; if we were to add more code before the Console。WriteLine() call;  

we would need to scroll through the text to find the message to change。 A variable is perfect for  

this; as we can define some data (the message to print); and then use it later in our program。 
返回目录 上一页 下一页 回到顶部 0 0
未阅读完?加入书签已便下次继续阅读!
温馨提示: 温看小说的同时发表评论,说出自己的看法和其它小伙伴们分享也不错哦!发表书评还可以获得积分和经验奖励,认真写原创书评 被采纳为精评可以获得大量金币、积分和经验奖励哦!