is not the responsibility of the IWorkbook interface; but of the IWorkbook interface implementation。
■Note The IWorkbook interface does not provide a Clear() method to reset the workbook and delete all
of the referenced worksheets。 It would seem logical to have a Clear() method; but in a garbage…collected
environment; that’s pletely unnecessary。 If you don’t want to use a workbook anymore; just don’t refer
ence it; and the garbage collector will take care of the rest。 Think of it as having the option of serving dinner
to your guests on real plates or paper plates。 Real plates might seem better; but they break and you need to
wash them。 Paper plates are used once and thrown away。 Of course; with paper plates you have recycling
issues that you don’t have in ; because the memory is recycled for you。
The property Identifier identifies the workbook represented by the current IWorkbook
object。 The identifier might be a path or file name and is pletely dependent on the imple
mentation of IWorkbook。
…………………………………………………………Page 321……………………………………………………………
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 299
The default property; Item; is the primary way of getting and retrieving worksheets; where
each worksheet is referenced using a string identifier。 The identifier does not need to be a string—
it could have been a custom type; enumeration; or interface that is implemented。 Using a string
keeps things simple; but there are maintenance issues。
Let’s say all workbooks have a configuration worksheet。 So for most of the code; the string
identifier 〃configuration〃 is used。 However; a new programmer decides to use 〃Configuration〃
(with a capital C)。 This slight change will cause problems because 〃configuration〃 is meant to
have a lowercase c。 Here’s the example:
Dim workbook As IWorkbook
Dim worksheet1 As IWorksheetBase = workbook(〃configuration〃)
Dim worksheet2 As IWorksheetBase = workbook(〃Configuration〃)
This approach uses a buffer that is typed in by hand and is considered hard…coded。 The
preferred alternative is to hard…code a structure that is then referenced throughout the source
code; like this:
Public Module WorksheetIdentifiers
Public Const Configuration As String = 〃configuration〃
End Module
Dim workbook As IWorkbook
Dim worksheet1 As IWorksheetBase = workbook(WorksheetIdentifiers。Configuration)
Dim worksheet2 As IWorksheetBase = workbook(WorksheetIdentifiers。Configuration)
The module WorksheetIdentifiers still contains a hard…coded string buffer; but this buffer
has been centralized to a single location。 The workbook default property references the identifier
within the class WorksheetIdentifiers。 Thus; if you change the class WorksheetIdentifiers; you
also change the identifiers used by the default property。 That way; the chances of having a typo
break an application are reduced。
Let’s get back to the IWorkbook interface and; in particular; the default property。 The default
property is of the type IWorksheetBase; which is a bit tedious because the IWorksheetBase inter
face is a rudimentary interface and most likely not the interface that you will use; because we
have IWorksheet(Of BaseType); and therein lies the problem。 To get an IWorksheet instance;
you would need a cast; as follows:
Dim workbook As IWorkbook
Dim worksheet As IWorksheet(Of String) = _
TryCast(workbook(WorksheetIdentifiers。Configuration); IWorksheet(Of String))
The bolded code is the cast you need each and every time you want to reference an IWorksheet
instance。 The cast is not a big deal; but it is tedious。 I personally would prefer being able to call
a property; method; or default property that returns the type I want。
Therein lies the problem; because the way I want to use a default property; property; or
method is as a mixed type; and you can’t define a default property with mixed types。 You can
define a default property that is a fixed type。 To understand this problem; look at the following
declaration of a workbook; which does pile。
…………………………………………………………Page 322……………………………………………………………
300 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
Imports System。Reflection
Imports Devspace。Trader。mon
_
Public Interface IWorkbook(Of BaseType)
Inherits IDebug
Default Property Item(ByVal identifier As String) As IWorksheet(Of BaseType)
ReadOnly Property Identifier() As String
End Interface
In this declaration of IWorkbook; a generics type is used; but then the problem is that
the default property can return only IWorksheet instances of a single type; such as Double or
String。 Remember that there are multiple worksheet types (as illustrated in Figure 11…2)。
What we want to do is use method…level generics declarations; like this:
_
Public Interface IMixedType
Default Property Item(Of BaseType)(ByVal identifier As String) _
As IWorksheet(Of BaseType)
End Interface
The problem with the method…level declaration is that it does not pile。 There are two
ways to declare a generics parameter。 The first is what you have seen the most often; and
that is at the type level:
Class MyType(Of GenericType)
End Class
Declaring at the type level means that whenever you use the type and specify a type for the
generics parameter; MyTyp
小说推荐
- 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章