|
在用vb开发平台的,我们时常会遇见这样一个问题
strName = cmbUser.SelectedItem.Value
'//
cmbUser.SelectedItem.Value = strName
上面两个语句看是正常,可是有时我们在跑动程序是“无缘无故” 在这里报错!!!
什么原因??因为vb中有种DbNull数据类型,在帮助文档里面这种函数的解释不够清晰
很多人无法立即,到这里就束手无策,咋办??
简单 可以写个函数
private function DbNullTo(byval strSource as string,byval repl as string ) as string
if IsDbNull(strSource) = true then
return repl
else
return strSource
end if
end sub
这是我们可以保证程序中不会出现有DbNuLL 类型时会出错(上面的情况只处理了字符型)
我们可以中 重载的方式来写上面的函数 这样可以处理其他的情况
但是 这样的情况下 代码会增加
怎么办??
我们利用vb里面的 object 类型来试试
private function DbNullTo(byval datSource as object,byval datRepl as object ) as object
if IsDbNull(datSource) = true then
select case datRepl .GetType.Name
case "Int32" ' Integer
return CInt(datRepl )
case "Single" 'Single
case "Double" ' Double
....
end select
end if
select case datSource.GetType.Name
case "Int32" ' Integer
return CInt(strDatSource)
case "Single" 'Single
case "Double" ' Double
....
end select
end function
需要注意的是 上面的代码中 双引号类的类型名要区分大小写 ,当然上面的写法不是最好的方法
但是可以一劳永逸 |
|