Creating a function similar to CType...I'm stuck.

  • Thread starter Thread starter Harry F. Harrison
  • Start date Start date
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?
 
Why don't you try the "Convert" class. This statement works fine...

Dim s As String = Convert.ToString(DBNull.Value)

and sets s = ""
 
Harry F. Harrison said:
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...

I think you're out of luck. CType is a built-in language construct, not
a regular method, so there's nothing you can do to emulate it. On the
bright side, if all you want to do is convert to a string, you're in luck.
Just use the ToString method, and that should work (it will convert DBNull
values to an empty string).

Jeremy
 
You can't. Each function has a return type declared. If that type is Object,
it can return anything - but the reference that will be returned will be of
type Object. Once you get it, you will still have to cast it, to whatever
it's real type is.
 
Sure, I can do that, but I didn't really want to call different methods for
different datatypes.

i.e., Convert.ToBoolean() Convert.ToInt16(), Convert.ToSingle(), etc...

An alternative would be to create a Sub with a ByRef parameter:

CastFromDB(Row("FirstName"), strName)

because strName is a specific type, I can then overload the method.

I was just looking for a way to do this as a function, similar to CType.

strName = CastFromDB(Row("FirstName"), String) --- and return a string
object
 
There's really no way to do this without overloading the method call to
return a specific type of object. CType is an inline function, but it would
have to be overloaded to return a specific type for each cast. If you simply
return "Object" you'll still have to cast this to whatever specific type you
want. If you really want to do this you could create an overloaded function
based on the "Convert" class methods. Of course, this may be more trouble
than it's worth. It just depends if a little more work now will save you
time elsewhere by only having to call a single function to do the
conversions...
 
In my function CastFromDB(ByVal Object, ByVal Type) as Object
it checks if the object passed is Nothing or dbNull.

if the Type I pass is Boolean, I return false
if it's numeric, I return 0
if it's a string, I return ""
otherwise i return the object unmodified.

Thanks for the info. I've never really gotten used to dbNull, since going
from VB-DOS to VB3.
In .Net, it's even worse that they made the string data type a reference
type, and can have a value of Nothing. I understand why, it's just
irritating to go back and modify a lot of code that used to work, just so
that VB can be just like all of the other languages.
 
I think that is the whole problem - I can't create an overloaded function
unless I have different data types.

Function myFunction(GetType(String))

doesn't give me a unique signature, so I can't overload it and return a
specific datatype.

If I do this:

Function myFunction(integer)

of course I can overload that, but in my case, I'm primarily working off of
objects coming out of a datarow, and it's in a low-level generic DAL, and
I'm using the schema of the database (retrieved at run-time), to set the
correct type.

Guess we wait for Whidbey, oops - I mean Visual Studio 2007. :)
 
I was just wondering what you're doing with the values that are returned
from this function. If you are assigning them to a variable then you could
just pass the variable into the function ByRef as object and cast it to the
correct type in the "Select Case" or "If" section of the function that
determines the type. Then you could simply set the variables value and
return a True or False value to indicate if the conversion was successful.
If you're creating an SQL statement you could always return a string. It
depends in part where the value will end up...
 
Thanks for bouncing this back and forth. There's so many small details in
..net - i think it's a bigger change than VB-DOS to VB Windows was 12 yrs
ago, because the whole foundation has changed.
 
Back
Top