这是我第一次尝试在vb.net编写一个oop程序。该程序的基本思想是使用Excel工作表作为数据源,然后更新mysql数据库。为了实践OOP的原则,我创建了一个名为motorbike和financeExample的类型。字符串VB.Net类型转换
我使用下面的连接字符串,它的数据作为文本仅只是因为似乎有一个问题阅读1198
Dim xcelConnString As String =
"Provider=Microsoft.Jet.OLEDB.4.0;" _
& "Data Source=R:\newBikeUpdates\motorcyclesLatest.xls;" _
& "Extended Properties=""Excel 8.0; IMEX=1"""
所以我在读值模型,然后将其分配给摩托车和financeExample对象的属性。
随着vbscript的接地,它非常快速地变得非常清楚,数据类型并不像我第一次想到的那么容易。所以,我因此写了将在一个属性分配一个值的点返回正确类型的函数:
motorcycle.category = checkValue(xcelDA("Category")) ' String expected
motorcycle.weight = checkValue(xcelDA("Weight")) 'String passed and Integer should be returned.
因此,这里的功能:
Function checkValue(ByVal v As Object)
Dim ret
If Not IsDBNull(v) Then
If Double.TryParse(v, ret) Then
Debug.WriteLine("v is Double")
Return ret
ElseIf Integer.TryParse(v, ret) Then
Debug.WriteLine("v is an Integer")
Return ret
ElseIf v.GetType Is GetType(String) Then
Debug.WriteLine("v is a string")
Return CStr(v)
Else ' Presumption of string type
Debug.WriteLine("v is Other")
Return v
End If
Else
Debug.WriteLine("v is DBNull")
ret = ""
Return ret
End If
End Function
和字符串元素工程类别和字符串被返回。但是,当它分配权重值时,我得到一串“417”传入,但double.tryparse(v,ret)或integer.tryparse(v,ret)元素都不起作用。当使用debug作为v.getType()时,VS2010报告v为字符串/ system.string。我甚至试过在integer.tryParse(“417”,ret)中传递调试,并且这可以工作,但由于某种原因它不会与v参数一起使用。
我也试过integer.tryParse(v.toString,ret),这也不起作用。
所以任何人可以点我在正确的方向请如何克服这个问题,因为CINT似乎不工作(VBscript的接地,对不起!)
许多在此先感谢! 格雷厄姆
好,我正要发表评论说:“正常工作对我!”我基本上通过“451”作为参数checkValue。 – Constanta 2013-03-19 12:16:49
感谢您的关注。我仍然遇到类型转换问题。有没有关于这个问题的建议,以及如何最好地接近它。 – EvilKermitSaurus 2013-03-19 13:14:12
如果你有更多类似的问题,请张贴一些代码 – Constanta 2013-03-19 14:46:19