worksheet and define the actual type using generics。 The problem with this solution is
that a workbook would define a collection of mixed types。
You might be tempted to believe that Worksheet(Of Double) and Worksheet(Of String) are
of the type Worksheet(Of BaseType); and thus are all a single type。 This is not the case; because
with generics; a type that hasn’t been concretized is not a type at all。 Think of it as being
an almost type; and to make the program work; you need to concretize everything。 Figure 11…2
shows two concrete types: Worksheet(Of Double) and Worksheet(Of String)。 These are two
different types。 The two different types make it plicated for the workbook; because the
workbook wants to maintain a single collection of worksheets。 If we assume for the moment
that the worksheet interface is defined as follows:
Interface IWorksheet(Of BaseType)
End Interface
the workbook could reference the worksheet as this collection:
Dim _worksheets As List(Of IWorksheet(Of BaseType))
However; that reference is inplete; and the piler would want to know what BaseType
references。 To keep your options open; one solution is to not plete the BaseType; but let the
user of workbook figure things out; thus defining workbook as follows:
Class Workbook(Of BaseType)
Dim _worksheets As List(Of IWorksheet(Of BaseType))
End Class
…………………………………………………………Page 317……………………………………………………………
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 295
This solution seems to be a good one; but; in fact; it’s passing the buck。 The solution does
not address the problem of Figure 11…2 and forces the end user to solve it。 The core problem is
that Figure 11…2 uses generics to define worksheets of specific types; which means mixed
types that need to be addressed by the workbook。 In other words; a workbook can contain only
spreadsheets of a certain type; as in this example:
Workbook(Of String) workbook1
Workbook(Of Double) workbook2
It would seem that generics make everything more plicated。 However; there’s
more to this than first appears。
generics have not made things more plicated; but rather have required us to be
more explicit about what we actually want to do。 We want to be able to define specific work
sheet types; which means we have a mixed list of types that the workbook must manage。 As
explained in Chapter 9; non… generics list types cannot control whether or not a list contains
mixed types。
To solve the worksheet problem; we need to put on our object…oriented thinking caps。
First; what is a worksheet? It’s a spreadsheet that fulfills the role of a two…dimensional thing;
and it applies to all worksheets regardless of types。 Therefore; the first interface to define is a
base worksheet; as follows (do this in the ServerSideSpreadsheet project; as well as adding a
reference to the Devspace。Trader。mon project):
Imports Devspace。Trader。mon
Public Interface IWorksheetBase
Inherits IDebug
Sub Dimension(ByVal rows As Integer; ByVal cols As Integer)
ReadOnly Property MaxCols() As Integer
ReadOnly Property MaxRows() As Integer
End Interface
The interface definition of IWorksheetBase has one method and two properties。 The method
Dimension() is used to assign the maximum rows and columns of the individual spreadsheet。
The properties MaxRows and MaxCols return the maximum rows and columns。 The properties
and method have nothing to do with the specific type managed by the worksheet; but the inter
face manages to uniquely identify the instance as being a type of spreadsheet。
In the workbook code; the list of worksheets would be defined as follows:
Private _worksheets As IDictionary(Of String; IWorksheetBase) = _
New Dictionary(Of String; IWorksheetBase)
Now the workbook knows it has a series of worksheets; but the workbook does not know
or care about the types of the worksheets。 When users of the workbook want to manipulate an
individual worksheet; they can retrieve the worksheet from the workbook; but the users need
to know the worksheet’s type。
The spreadsheet is typically addressed using rows and columns; but to simplify declara
tions; there is also the ability to define something called the SheetCoordinate; which is a type
that has a row and column。 The SheetCoordinate is defined in ServerSideSpreadsheet as follows:
…………………………………………………………Page 318……………………………………………………………
296 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
Public Structure SheetCoordinate
Public Row As Integer
Public Column As Integer
Public Sub New(ByVal row As Integer; ByVal column As Integer)
If (row 《 0) Then
Throw New ArgumentOutOfRangeException(〃Row is below zero〃)
End If
If (column 《 0) Then
Throw New ArgumentOutOfRangeException(〃Column is below zero〃)
End If
Me。Row = row
Me。Column = column
End Sub
Public ReadOnly Property OneUp() As SheetCoordinate
Get
Return New SheetCoordinate((Me。Row 1); Me。Column)
End Get
End Property
Public ReadOnly Property OneDown() As SheetCoordinate
Get
Return New SheetCoordinate((Me。Row + 1); Me。Column)
End Get
End Property
小说推荐
- 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章