Parameterizing Return Types

S

Simon Woods

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
 
G

Göran Andersson

Simon said:
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
 
S

Simon Woods

Göran Andersson said:
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
 

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

Top