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
小说推荐
- oracle从入门到精通(PDF格式)
- -Page 1-Oracle 从入门到精通-Page 2-资源来自网络,仅供学习 Oracle 从入门到精通一、SQL 8
- 其他
- 最新章:第37章
- C语言游戏编程从入门到精通(PDF格式)
- -Page 1-Page 2-Page 3-Page 4-Page 5-Page 6-Page 7-Page 8-Page 9-Page 10-Page 11-Page 12-Page 13-Page 14
- 其他
- 最新章:第4章
- Java编程思想第4版[中文版](PDF格式)
- -Page 1-Page 2《Thinking In Java》中文版作者:Bruce Eckel主页:http/BruceEckel.编译:Trans Bot主页:http/memberease~transbot致谢-献给那些直到现在仍在孜孜不倦创造下一代计算机语言的人们!指导您利用万维网的语言进
- 其他
- 最新章:第295章
- 深入浅出MFC第2版(PDF格式)
- -Page 1-Page 2-山高月小山高月小 水落石出水落石出山高月小山高月小 水落石出水落石出-Page 3-深入淺出MFC(第版 使用Visual C 5.0 MFC 4.2)Dissecting MFC(Second Edition Using Visual C 5.0 MFC 4.2)侯俊
- 其他
- 最新章:第309章
- VC语言6.0程序设计从入门到精通
- -Page 1-Visual C 6.0 程序设计从入门到精通求是科技 王正军 编著
- 其他
- 最新章:第136章
- SQL 21日自学通(V3.0)(PDF格式)
- -Page 1-SQL 21 日自学通(V1.0 翻译人 笨猪目录目录 1译者的话 14第一周概貌 16从这里开始 16
- 其他
- 最新章:第170章
- 2008年青年文摘精编版
- 作者:中国青年出版社“初恋”的惩罚.作者:凡 凡 文章来源《真情》2005年第4期 点击数:6608 更新时间:2005-6-5过了年,我就十八岁了。离高考只剩下四个多月了。这一段,班里的男女生相互间递纸条、写情书、约会等地下活动慢慢的多了起来。我这个“尖子生”也突然感到了不安、慌乱,并且自责。不知
- 文学名著
- 最新章:第230章
- SQL语言艺术(PDF格式)
- -Page 1-SQLSSQQLL语言艺术内容介绍本书分为12章,每一章包含许多原则或准则,并通过举例的方式对原则进行解释说明。这些例子大多来自于实际案例,对九种SQL经典查询场景以及其性能影响讨论,非常便于实践,为你数据库应用维护人员阅读。资深 SQL 专家 Stéphane Faroult倾力打
- 其他
- 最新章:第27章
- JMS简明教程(PDF格式)
- -Page 1-JMS1.1规范中文版卫建军2007‐11‐22-Page 2
- 其他
- 最新章:第28章