《VB2008从入门到精通(PDF格式英文版)》第195章


The Important Stuff to Remember 
In this chapter; you learned about the basics of LINQ and how to write queries。 Here are the key
points to remember: 
o LINQ is an API that sits on top of other technologies such as Visual Basic objects; rela
tional databases; and XML documents。
o LINQ can work effectively only if the underlying data source technology has been opti
mized for LINQ。 Otherwise; you are left with having to load a single record set and then
manipulate that record set。 
o Regardless of the data source; the techniques used to query and write LINQ are identical。 
o When manipulating LINQ objects; the methods and properties associated with the
various data sources are different。 For example; when searching XML documents; you
can use XML Document Object Model (DOM) methods and properties that are not avail
able when manipulating plain…vanilla objects。 
o LINQ is not just a syntax; but a series of extension methods associated with sets of data。
The methods allow for more sophisticated data pipelining and processing of
information。 
…………………………………………………………Page 438……………………………………………………………
416 CH AP T E R 1 5 ■ L E A R N I N G A B OU T L I N Q 
Some Things for You to Do 
The following are two exercises to help you apply what you’ve learned in this chapter。 
1。 The solution for finding a frequency presented in this chapter went from text to text to
calculate the statistics。 Can you think of another approach that would require minimal
changes in the interface structure? Hint: the way the objects were parsed into objects
borrowed code from another application。 Could that other application be used somehow?
2。 You saw a LINQ query embedding another LINQ query when finding the frequency of
two numbers。 Rewrite the code to generate the frequency of all binations of single;
pairs; and triples。 
…………………………………………………………Page 439……………………………………………………………
C H A P T E R 1 6 
■ ■ ■ 
Learning About Other Visual
Basic Techniques 
This last chapter in the book is about tying up loose ends。 The techniques discussed in this
chapter are those that you will use in specific situations。 This chapter covers the following
topics: 
o How to use arithmetic operators to manipulate numbers 
o How to overload operators 
o When you might use the GoTo statement
o How to use generics constraints 
o How to use nullable types
o How to use partial classes and methods
Operators 
You have seen various operators used in examples throughout the book; such as the assignment
operator (a = 3); and the logical operators ( If( a = b))。 Visual Basic has many more arithmetic
operators that you can use to process types。 You can also define custom operators。
Using Arithmetic Operators 
The subtraction ( …); multiplication (*); and division (/) operators are typically applicable to
only numeric values。 These operators are directly parable to the mathematical operators
you learned about in elementary school。 Let’s look at what the other arithmetic operators do。 
Addition 
The addition (+) operator is used to indicate the addition of two values; like this: 
a = c + 1 
The addition has a left…hand side and right…hand side; separated by the equal sign (=)。
On the right…hand side; the variable c is added to 1 and assigned to the variable a。 
417 
…………………………………………………………Page 440……………………………………………………………
418 CH AP T E R 1 6 ■ L E A R N I N G A B OU T O TH E R V IS U AL B A SI C TE C H N IQ U E S 
The notion of a left…hand side and a right…hand side as two separate parts is important
when you consider this code: 
a = a + 1 
In the example; the variable a is added with the value 1 and assigned to the variable a; but
these operations do not happen at the same time; they happen sequentially。 First; the right
hand side is executed; and then the left…hand side is executed。 By executing the left…hand side;
the existing value of the variable a is overwritten。 
Let’s consider another example: 
b = a = a + 1 
If you were to run this code; b would not be assigned; because the example has mixed
operators; which translates to this: 
Dim b As Integer = CInt(…((a = (a + 1)) 》 False)) 
The result is not what you expected; and not even close to what you were trying to achieve
(and isn’t even allowed with Option Strict set to On)。 What this illustrates is that you need to be
careful with operators。 
Bitwise Operators 
Bitwise operators are used to access and manipulate individual bits in a whole number。
As you learned in Chapter 2; the puter sees numbers as binary; with only two unique
identifiers。 Thus; whole numbers could be viewed as arrays of Boolean values; as there are only
two valid values: 1 and 0 or True and False。
Let’s look at example of using the bitwise operators。 Say that you want to know whether a
person is tall; wears hats; and runs slowly。 Using Boolean data members; you would write the
following code。 
Class PersonWithAttributes
Public IsPersonTall As Boolean 
Public WearsHats As Boolean 
Public RunsSlowly As Boolean 
小说推荐
返回首页返回目录