469,645 Members | 1,603 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,645 developers. It's quick & easy.

Parameterizing Return Types

Hi

I am storing data type information in a config file as a string. I think
the queation I want to ask is "Is it possible to convert it into a
Generic Type".

So, e.g., I may have a selection of objects in a dictionary

Object 1 holds and returns a Boolean
Object 2 holds and returns a String
Object 3 holds and returns an Integer

However, each object gets it return type from the config file. I know
that I will have to do a Select Case on the config value at some stage, e.g.

Select Case Config.ReturnType
Case "Boolean"
Return GetType(Boolean)
Case "String"
Return GetType(String)
etc etc

I was wanting to store this information so I could issue, e.g.

dim myvar = Object1.DataValue(Of Object1.DataType)

(yes I know this command is illegal but it sort of shows what I am
trying to achieve)

so all the type information is completely in each object and I don't
have to expose

If Object1.IsBoolean Then
Return Object1.BooleanValue
etc etc

I was hoping not to have to implement "DataValue() as Object" but have
"stronger" typing on it

I don't know whether this is possible, or in fact whether I am going
round the houses to something which could be implemented in a much
easier way.

Thx in advance

Simon



Jun 27 '08 #1
2 795
Simon Woods wrote:
Hi

I am storing data type information in a config file as a string. I think
the queation I want to ask is "Is it possible to convert it into a
Generic Type".

So, e.g., I may have a selection of objects in a dictionary

Object 1 holds and returns a Boolean
Object 2 holds and returns a String
Object 3 holds and returns an Integer

However, each object gets it return type from the config file. I know
that I will have to do a Select Case on the config value at some stage,
e.g.

Select Case Config.ReturnType
Case "Boolean"
Return GetType(Boolean)
Case "String"
Return GetType(String)
etc etc

I was wanting to store this information so I could issue, e.g.

dim myvar = Object1.DataValue(Of Object1.DataType)

(yes I know this command is illegal but it sort of shows what I am
trying to achieve)

so all the type information is completely in each object and I don't
have to expose

If Object1.IsBoolean Then
Return Object1.BooleanValue
etc etc

I was hoping not to have to implement "DataValue() as Object" but have
"stronger" typing on it

I don't know whether this is possible, or in fact whether I am going
round the houses to something which could be implemented in a much
easier way.

Thx in advance

Simon
You could possibly use a generic property, but you would have to specify
the data type at compile time anyway. A property can only return a
specific data type, and it has to be known at compile time.

Also, you need to declare the variable as that data type also, otherwise
it will become an Object variable. (In the latest version of VB it can
derive the data type from the return type of the property, but the type
still has to be known at compile time.)

So, there isn't really much difference between:

Dim myvar As Boolean = Object1.DataValue(Of Boolean)

and just use specific methods for the data types:

Dim myvar As Boolean = Object1.BooleanValue
--
Göran Andersson
_____
http://www.guffa.com
Jun 27 '08 #2
Göran Andersson wrote:
Simon Woods wrote:
>Hi

I am storing data type information in a config file as a string. I
think the queation I want to ask is "Is it possible to convert it into
a Generic Type".

So, e.g., I may have a selection of objects in a dictionary

Object 1 holds and returns a Boolean
Object 2 holds and returns a String
Object 3 holds and returns an Integer

However, each object gets it return type from the config file. I know
that I will have to do a Select Case on the config value at some
stage, e.g.

Select Case Config.ReturnType
Case "Boolean"
Return GetType(Boolean)
Case "String"
Return GetType(String)
etc etc

I was wanting to store this information so I could issue, e.g.

dim myvar = Object1.DataValue(Of Object1.DataType)

(yes I know this command is illegal but it sort of shows what I am
trying to achieve)

so all the type information is completely in each object and I don't
have to expose

If Object1.IsBoolean Then
Return Object1.BooleanValue
etc etc

I was hoping not to have to implement "DataValue() as Object" but have
"stronger" typing on it

I don't know whether this is possible, or in fact whether I am going
round the houses to something which could be implemented in a much
easier way.

Thx in advance

Simon

You could possibly use a generic property, but you would have to specify
the data type at compile time anyway. A property can only return a
specific data type, and it has to be known at compile time.

Also, you need to declare the variable as that data type also, otherwise
it will become an Object variable. (In the latest version of VB it can
derive the data type from the return type of the property, but the type
still has to be known at compile time.)

So, there isn't really much difference between:

Dim myvar As Boolean = Object1.DataValue(Of Boolean)

and just use specific methods for the data types:

Dim myvar As Boolean = Object1.BooleanValue
OK then ... thx ... Göran
Jun 27 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Alex Vinokur | last post: by
9 posts views Thread by Ann Huxtable | last post: by
2 posts views Thread by Arne | last post: by
reply views Thread by RobR | last post: by
6 posts views Thread by miked | last post: by
8 posts views Thread by Alex Vinokur | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.