Generics

M

Michel Racicot

When I do the following piece of code, it seems that it's illegal ton
typecast a string into a generic result:

Private Function GetTypeName(Of t)(ByVal FieldName As String) As t
Dim MyResult As t

If TypeOf MyResult Is String Then
MyResult = CType("Test", t)

Else

' Some other stuff
End If
Return MyResult
End Function


What can I do to solve the issue?
 
A

Armin Zingler

Michel said:
When I do the following piece of code, it seems that it's illegal ton
typecast a string into a generic result:

Private Function GetTypeName(Of t)(ByVal FieldName As String) As t
Dim MyResult As t

If TypeOf MyResult Is String Then
MyResult = CType("Test", t)

Else

' Some other stuff
End If
Return MyResult
End Function


What can I do to solve the issue?


A String can not be converted to every type. What if T is a Form?

dim MyResult As Form

MyResult = ctype("test", Form)

How should this work?


Armin
 
M

Michel Racicot

What I'm trying to accomplish is a function that will handle the DBNulls
values and some other casts correctly according to our business logic. For
sure, I can create various overloads of the function for different
datatypes, but I think it will be better with generics.

for instance:

EmployeeId = HandleNullValue(of Integer)(MyRow("EMPLOYEE_ID"))
Name = HandleNullValue(of String)(MyRow("LAST_NAME"))

previously, we had:

EmployeeId = NullToInteger(MyRow("EMPLOYEE_ID"))
Name=NullToString(MyRow("LAST_NAME"))
 
A

Armin Zingler

Michel said:
What I'm trying to accomplish is a function that will handle the
DBNulls values and some other casts correctly according to our
business logic. For sure, I can create various overloads of the
function for different datatypes, but I think it will be better with
generics.

for instance:

EmployeeId = HandleNullValue(of Integer)(MyRow("EMPLOYEE_ID"))
Name = HandleNullValue(of String)(MyRow("LAST_NAME"))

previously, we had:

EmployeeId = NullToInteger(MyRow("EMPLOYEE_ID"))
Name=NullToString(MyRow("LAST_NAME"))


What is currently done inside NullToInteger, NullToString etc?
What do you expect from HandleNullValue?

Maybe you nedd something like this:

Shared Function HandleNullValue(Of T)(ByVal value As Object) As T
If value Is DBNull.Value Then
'...
Else
Return DirectCast(value, T)
End If
End Function




Armin
 
N

nak

Hi Michael

Why don't you just analyse the type of T again?

As Armin said, it won't support every type, so you shouldn't support
every type, that is poor coding.

...
If TypeOf MyResult is String Then
'//Check that the string can be converted into type 't' here before
doing so, if not, throw an exception
'//You could do this by making a small private method that accepts a
type and returns a Boolean
'//to let you know if it can be typecast from a string
Else
...

Also, personally, I'd use the Parse methods...

Boolean.Parse(stringvalue)
Integer.Parse(stringvalue)
Single.Parse(stringvalue)

etc. etc.

Nick
 
M

Michel Racicot

Casting objects to T is actually a pretty good idea!!!

It solved the problem
 
N

nak

for example////

Public Function canBeTypeCastFromString() As Boolean
Select Case GetType(t).ToString()
Case GetType(Integer).ToString(), _
GetType(Boolean).ToString(), _
GetType(Single).ToString()

Return(True)
Case Else
Return(false)
End Select
End Function

If TypeOf MyResult Is String Then
if(canBeTypeCastFromString())
MyResult = CType("Test", t)
else
'//fall over and choke
end if
Else
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top