H
Harry F. Harrison
I'm trying to create a somewhat generic function that cleans up data and
casts it to the correct .NET type coming from a DataRow object.
example implementation:
Dim strName as String
(with option strict off - simplified)
strName = Row("FirstName")
(with option strict - I need to CType it)
strName = CType(Row("FirstName"), String)
if Row("FirstName") = dbNull, I'll get an exception when I execute that
line, even with the CType.
So, I took a routine that I had from VB6, dusted it off, and tried using it.
Public Shared Function CastFromDB(ByVal objField As Object, ByVal DataType
As System.Type) As Object
End Function
The usage is almost identical to CType, except not quite as clean...
(option strict off)
strName = CastFromDB(Row("FirstName"), GetType(String))
(option strict on)
strName = CastFromDB(Row("FirstName"), GetType(String)),String)
To make a long story short, how does one create a function similar to CType,
where I don't have to use the GetType() function, and then be able to
overload it to return exact types, instead of returning an object, which
then requires me to CType it anyways...
It sounds like this feature is in Whidbey, but I don't want to have to wait
for generics.
Any ideas?
casts it to the correct .NET type coming from a DataRow object.
example implementation:
Dim strName as String
(with option strict off - simplified)
strName = Row("FirstName")
(with option strict - I need to CType it)
strName = CType(Row("FirstName"), String)
if Row("FirstName") = dbNull, I'll get an exception when I execute that
line, even with the CType.
So, I took a routine that I had from VB6, dusted it off, and tried using it.
Public Shared Function CastFromDB(ByVal objField As Object, ByVal DataType
As System.Type) As Object
End Function
The usage is almost identical to CType, except not quite as clean...
(option strict off)
strName = CastFromDB(Row("FirstName"), GetType(String))
(option strict on)
strName = CastFromDB(Row("FirstName"), GetType(String)),String)
To make a long story short, how does one create a function similar to CType,
where I don't have to use the GetType() function, and then be able to
overload it to return exact types, instead of returning an object, which
then requires me to CType it anyways...
It sounds like this feature is in Whidbey, but I don't want to have to wait
for generics.
Any ideas?