…………………………………………………………Page 327……………………………………………………………
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 305
Public Interface IWorksheetSerialize
Sub AssignCellState(Of ValueType)(ByVal row As Integer; _
ByVal col As Integer; ByVal value As ValueType)
Sub AssignCellState(ByVal row As Integer; ByVal col As Integer; _
ByVal value As Object)
End Interface
The problem is that the function passes in as a parameter of one type and is assigned
to CellState; which is another undefined generics type。 The implementation of the
AssignCellState() method is as follows:
Public Sub AssignCellState(ByVal row As Integer; ByVal col As Integer; _
ByVal value As Object) Implements IWorksheetSerialize。AssignCellState
CellState(row; col) = DirectCast(value; BaseType)
End Sub
The code looks so simple and innocent; but it is actually masking many potential problems。
Here; the value of the parameter value is of type Object。 Then to convert the value to type
BaseType—because that is what CellState is defined to be—you use a cast。 For the most part;
this code will work; if value is the correct type。
Consider the following code; which would generate an exception。
Dim worksheet As Worksheet(Of Double) = New Worksheet(Of Double)()
worksheet。Dimension(10; 10)Dim buffer As String = 〃hello; world〃
worksheet。AssignCellState(1; 2; buffer)
The variable worksheet is declared to be of type Worksheet(Of Double)。 When the method
AssignCellState() is called; the cell 1; 2 is assigned to be type String。 Calling AssignCellState() is
not a problem; but in the implementation; the assignment will fail。 You can’t willy…nilly assign
a String to a Double。 Of course; this begs the question; “Why have a function of type Object?”
Sometimes you can’t get around it; and you need to write a general object method。 The proper
way to call the method would be as follows:
Dim worksheet As Worksheet(Of Double) = New Worksheet(Of Double)()
worksheet。Dimension(10; 10)
Dim buffer As String = 〃hello; world〃
worksheet。AssignCellState(1; 2; Double。Parse(buffer))
The bolded code shows that the string buffer is parsed by the Double。Parse() method;
converting a string buffer into a Double value。 Of course; the conversion will fail; since the string
buffer represents a string; but that is another problem。
Another way of solving the problem is to avoid the object altogether and declare the method
as being a generics method。 The advantage of the generics method is that you could
execute in a type…safe manner without explicitly forcing the user to implement parsing routines。
Consider the following modified generics method declaration of AssignCellState()。
…………………………………………………………Page 328……………………………………………………………
306 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 Sub AssignCellState(Of ValueType)(ByVal row As Integer; _
ByVal col As Integer; _
ByVal value As ValueType) _
Implements IWorksheetSerialize。AssignCellState
If GetType(BaseType)。IsAssignableFrom(GetType(ValueType)) Then
CellState(row; col) = DirectCast(DirectCast(value; Object); BaseType)
ElseIf TypeOf (value) Is String And _
GetType(Double)。IsAssignableFrom(GetType(BaseType)) Then
Dim obj As Object = DirectCast(value; Object)
Dim dValue As Double = Double。Parse(CStr(obj))
Dim objDValue As Object = CType(dValue; Object)
CellState(row; col) = DirectCast(objDValue; BaseType)
Else
Throw New InvalidCastException(〃Could not perform conversion〃)
End If
End Sub
The cell state to be assigned is a generics parameter and defined to be the type ValueType。
You can only guess what ValueType is; it is determined when the method AssignCellState(Of )()
is called。 For example; suppose this method call is made:
Dim buffer As String = 〃hello; world〃
worksheet。AssignCellState(1; 2; buffer)
The type for ValueType will be String; even though you have not explicitly specified it。 One
of the things that is possible with generics methods is that types can be deduced implic
itly。 The following would be an explicit usage of AssignCellState(Of )()。
Dim buffer As String = 〃hello; world〃
worksheet。AssignCellState(Of String)(1; 2; buffer)
Knowing that ValueType is String; AssignCellState(Of )() will then first check if
ValueType can be assigned to BaseType:
If GetType(BaseType)。IsAssignableFrom(GetType(ValueType)) Then
This code is rather clever; because it uses what is known as reflection to determine if one
type can be assigned to another type。 Essentially; it asks if it is OK via a cast to assign value to
CellState。 You could try to do this without the If statement; but then you risk an unnecessary
exception。 If it is permissible to assign; then the assignment is done via a two…step casting:
CellState(row; col) = DirectCast(DirectCast(value; Object); BaseType)
Here; you first convert the type to an Object; and then convert the type to BaseType; which
happens to be the type that the spreadsheet is declared as。 It is absolutely imperative that the
cast to the object is added; otherwise; the Visual Basic piler will plain that the cast is
not possible。
But suppose tha
小说推荐
- 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章