按键盘上方向键 ← 或 → 可快速上下翻页,按键盘上的 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