Variable function return type

  • Thread starter Thread starter John
  • Start date Start date
J

John

Hi

I have a function that needs to return value from some column in a table.
The column type is not known in advance. Is it possible for function to
return a value whose type would be determined at runtime? If so, how?

Thanks

Regards
 
John said:
Hi

I have a function that needs to return value from some column in a
table. The column type is not known in advance. Is it possible for
function to return a value whose type would be determined at
runtime? If so, how?

The type is Object, so the return type is Object, too.


Armin
 
John said:
Hi

I have a function that needs to return value from some column in a table.
The column type is not known in advance. Is it possible for function to
return a value whose type would be determined at runtime? If so, how?

Thanks

Regards

Have your function return be of type "Object". You will need to use "Is
<type>" to determine the actual return type and then use CType to coerce it
to the correct actual type before you can use the result.

Mike Ober.
 
Cor Ligthert said:
John,


Just curious, how did you create that? In my idea is that hard to do in
.Net.

Cor

This is easy to do, even with Option Strict On. The MS Office interop
classes are full of functions that return "Object" instead of the actual
types.

Mike Ober.
 
Michael D. Ober said:
Have your function return be of type "Object". You will need to use "Is
<type>" to determine the actual return type and then use CType to coerce
it to the correct actual type before you can use the result.

I think you wanted to type 'If TypeOf ... Is Then...', typically written as:

\\\
If TypeOf Foo Is Bar Then
Dim f As Bar = DirectCast(Foo, Bar)
...
End If
///

In addition, check out the 'TryCast' statement, which will return 'Nothing'
if the cast doesn't succeed.
 
Michael,

To me you write that it has no type and then you tell that you can get a
type, what is the truth, has it a type or does it have no type?

I am interested in a var without a type, that is where I am curious about.

Cor
 
Cor Ligthert said:
Michael,

To me you write that it has no type and then you tell that you can get a
type, what is the truth, has it a type or does it have no type?

I am interested in a var without a type, that is where I am curious about.

Cor

I've never heard of a variable without a type other than in typeless
languages. You misunderstood my comments - if you need to return multiple
types from a function, return type "Object". The calling code must then
cast it to the correct type before anything can be done with it. Also, as I
stated, the MS Office Interop assemblies are full of functions that return
"Object" and not the actual type. I assume that this is because the COM
world is very type limited, and the MS Office Interop assemblies must
ultimately call COM interfaces.

Mike.
 
Back
Top