using the Calculate() method。 Had the GetCellState() method been used; the average might
not have been calculated; and thus the difference calculation would have been corrupted。
But having each and every cell calculate the average whenever a small change is made is a
waste of resources; since the change might not affect a cell that is recalculated。 To avoid this;
built into the spreadsheet is a version…control mechanism that calculates a cell to the latest
version。 Then; if another calculation is called with the same version number; the value is retrieved
from the cell state。 The following is the code from Worksheet(Of BaseType) that manages the
version number。
Public Function Calculate(ByVal row As Integer; ByVal col As Integer) As BaseType _
Implements IWorksheet(Of BaseType)。Calculate
If (CurrVersion 》 CalculationVersion(row; col)) Then
CellState(row; col) = Cells(row; col)(Me; row; col)
CalculationVersion(row; col) = CurrVersion
End If
Return CellState(row; col)
End Function
…………………………………………………………Page 333……………………………………………………………
CH AP T E R 1 1 ■ L E A R N IN G AB O U T 。 N E T G E N E R I CS 311
Public Sub Calculate() Implements IWorksheet(Of BaseType)。Calculate
CurrVersion += 1
Dim row As Integer
For row = 0 To Cells。GetLength(0) 1
Dim col As Integer
For col = 0 To Cells。GetLength(1) 1
If Cells(row; col) IsNot Nothing Then
Calculate(row; col)
End If
Next col
Next row
End Sub
Calling the method Calculate() without parameters indicates a desire to recalculate the
entire spreadsheet。 In the implementation of Calculate(); the variable CurrVersion; which
represents the version number of the current calculation; is incremented。 Then each cell is iter
ated; and if it exists; the individual cell form of Calculate() (Calculate() with the parameters row
and col) is called。 In the individual cell form of Calculate(); a check is made to see if the calcu
lation version number of the cell is the latest of the spreadsheet; if not; the cell’s lambda expression
is called。 After the new CellState has been assigned; the cell…state version number is incre
mented; and the cell state is returned。
■Note The calculation of the spreadsheet is nothing earth…shattering; and you might be tempted to argue
it is irrelevant to the scope of the book。 In fact; the calculation and its side effects are of major relevance。 With
lambda expressions; you have a form of asynchronous processing; very much like a spreadsheet。 The cell
calculations of the spreadsheet do not know when they will be called; and they cannot make assumptions
about the state。 Thus; when a lambda expression is created; the state at the time of the lambda expression
may not be the same as when the lambda expression is executed。 If you are not acutely aware of this potential
pitfall; you could have some major bugs in your code。
The Important Stuff to Remember
In this chapter; you learned how to use generics and also expanded your knowledge of
lambda expressions。 The main items to remember are as follows:
o generics code can use generics; or it can be code that provides types based on
generics。
o Performance and type…safe characteristics are primary reasons for using generics。
o You can use generics at the type level or at the method level。 Using generics
at the type level implies concretizing the type when the type is instantiated。 Using
generics at the method level implies concretizing the type when the method is called。
o Lambda expressions may be shared state or individual state; depending on how they are
declared and manipulated。
…………………………………………………………Page 334……………………………………………………………
312 CH AP T E R 1 1 ■ L E A R N I N G A B OU T 。 N E T G E N E R I CS
o For plex types; you should always implement ToString() as a way of figuring out the
state of an instance。
o Lambda expressions act asynchronously。 When you use them; remember to not make
assumptions of a particular state。
Some Things for You to Do
The following are some exercises that allow you to apply what you’ve learned so far。
1。 The Worksheet(Of BaseType) class always requires you to dimension the fixed…cell array
ahead of time。 Change this code so that the number of rows and columns can change
dynamically。 Remember that the focus is on performance; and the fastest approach is a
fixed…dimension array。
2。 The average calculation knows how many items there are by the row number。 Rewrite
the average calculation code so that the user of the average code does not need to deal
with the plexities of adding more elements; moving the cell calculation; and so on。
3。 The methods Calculate() and GetCellState() seem to do the same thing。 So; is there a
need to have separate methods? Answer the question; and then make any necessary
changes to the source code based on your answer。
…………………………………………………………Page 335……………………………………………………………
C H A P T E R 1 2
■ ■ ■
Learning About
Application Configuration
and Dynamic Loading
All of the examples in the preceding chapters have demonstrated how to use an application
with a specific ponent。 You knew which type to instantiate; which interface to use; and
which pro
小说推荐
- 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章
- JMS简明教程(PDF格式)
- -Page 1-JMS1.1规范中文版卫建军2007‐11‐22-Page 2
- 最新章:第28章
- SQL语言艺术(PDF格式)
- -Page 1-SQLSSQQLL语言艺术内容介绍本书分为12章,每一章包含许多原则或准则,并通过举例的方式对原则进行解释说明。这些例子大多来自于实际案例,对九种SQL经典查询场景以及其性能影响讨论,非常便于实践,为你数据库应用维护人员阅读。资深 SQL 专家 Stéphane Faroult倾力打
- 最新章:第27章