not possible。
But suppose that the type cannot be directly assigned。 Let’s say that value references a
String that contains a number。 Then; using reflection; you can verify what ValueType and
BaseType are and perform the conversion yourself:
…………………………………………………………Page 329……………………………………………………………
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 307
ElseIf TypeOf (value) Is String And _
GetType(Double)。IsAssignableFrom(GetType(BaseType)) Then
The first part of the If statement asks if value is of type String。 The second part asks if Double
can be assigned to BaseType。 If both are valid; it means the input is a String and the spread
sheet type is a Double; so to convert; you only need to call Double。Parse()。
Providing this automatic conversion functionality for the caller of the code does not save
code; but it centralizes it and makes it general。 The caller will not generally need to worry about
the most mon conversions; as they will happen automatically (you will have implemented
them)。 For those conversions that cannot be done; a cast exception is thrown; just as the orig
inal object…based AssignCellState() would have thrown an exception。
The assignment is a series of casts and conversions that you need to do so that you can
convert one type to another。 The main problem when converting types in this way is that the
method…level generics parameter is a type that is not related to the type…level generics
parameter。
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)
The first step is to convert the parameter into an object so that the cast operators that
expect objects will work without the Visual Basic piler generating errors。 The If statement
said that the input type is a String; and destination type is a Double。 Thus; the methods CStr()
and Double。Parse() are called to convert the input string to a double number。 Once the string…to
double functions have pleted; the CellState could be assigned; if it were not for the little
problem of the generics…type level parameter。 The next steps of casting using CType() and
DirectCast() are a boxing and unboxing of a value type。 This is necessary because the piler
considers everything as an object; and thus you need to first convert to an object and then
convert to the actual type。
■Note The steps to convert from one generics type to another generics type would seem
cumbersome; and to a degree they are。 Although the steps illustrated here are the minimum; they allow you
to convert one type to another。 The steps are dangerous in that if you don’t know ahead of time whether the
conversion is allowed; an exception could result。 In the example; an exception is almost impossible because
the types were checked ahead of time; and an appropriate plan of action was taken。
Overall; the AssignCellState(Of )() method with generics parameters provides the
ability to cleanly assign a value to the spreadsheet; and a clean and maintainable method to
perform a conversion。 This goes back to the original requirement of being able to mix types safely。
Overriding the ToString() Functionality
Debugging a data structure like a spreadsheet is a fairly plex task; because there is too
much data。 The Worksheet(Of ) class implements the ToString() method and generates a
string。 The string can be retrieved and then displayed using a method like Console。WriteLine()。
…………………………………………………………Page 330……………………………………………………………
308 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
Here is the implementation of worksheet’s ToString() function。
Public Overrides Function ToString() As String
Dim builder As New StringBuilder()
Dim row As Integer
For row = 0 To Cells。GetLength(0) 1
Dim needma As Boolean = False
If _generateRowCounter Then
needma = True
builder。Append(row)
End If
Dim col As Integer
For col = 0 To Cells。GetLength(1) 1
If needma Then
builder。Append(〃;〃)
Else
needma = True
End If
If ellState(row; col) IsNot Nothing Then
builder。Append(CellState(row; col)。ToString)
End If
Next col
builder。Append(ChrW(10))
Next row
Return builder。ToString
End Function
Because the worksheet can be large; StringBuilder is used to incrementally build a string
that is returned。
■Note The ToString() method is useful when you’re debugging or trying to perform analysis of the state
of an object without actually debugging the program。 Thus; for improved debugging or runtime analysis; always
implement ToString()。
Using the Spreadsheet
With the interfaces and implementations plete; it is possible to use the spreadsheet。 The
sample application is a spreadsheet that calculates the average of a set of numbers and then
calculates how far each number is from the average。
Calculating an Average
The spreadsheet calculates the average o
小说推荐
- 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章