codequest
asked on
Return Multiple Types from Function
Is it possible to write a function that would return values of multiple types, depending on input, and if so, how? This is what my newbie-brain came up with...
Function GetKeyValue(Key(s), DataType) as ???
Dim x as integer
Dim y as single
Dim z as string
--- get key value from DB using Key(s), and based on Type indicated in DB, recast or select it into either x, y, or z
If Datatype = "INT" return x
If Datatype = "SGL" return y
if Datatype = "STR" return z
End Function
Any guidance on this would be appreciated.
Thanks!
Function GetKeyValue(Key(s), DataType) as ???
Dim x as integer
Dim y as single
Dim z as string
--- get key value from DB using Key(s), and based on Type indicated in DB, recast or select it into either x, y, or z
If Datatype = "INT" return x
If Datatype = "SGL" return y
if Datatype = "STR" return z
End Function
Any guidance on this would be appreciated.
Thanks!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
if you are using .net 2.0 and if you want to try generic classes post your existing code here.
ASKER
Thanks for inputs. Lemme do the prelim read on generics and I'll get back here.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
gangwisch: Thanks for input. That's an interesting alternative...seems like pretty compact coding, too.
ASKER
What I've concluded is that there's no "very simple" way to do this. Generics might be it, though for my level of programming, they're past "very simple" already.
The inputs here have given me something to work with...I can see I'll have to run some test code to pick the best alternative.
Thanks for the ideas!
The inputs here have given me something to work with...I can see I'll have to run some test code to pick the best alternative.
Thanks for the ideas!
ASKER
At a higher level, what I'm trying to do is get control values from key/value pairs stored in the DB.
https://www.experts-exchange.com/questions/21918874/Design-of-Controls-Table.html
I'm leaning toward having multiple columns in the control record, one for each data type...so I'd have to tell the GetValueFunction which column to look it in any case.
I "ought to" know at the time I go get the value what type it will be when I use it, so I can say
dim x as string = GetKeyValue("myvaluekey", "String")
or
dim y as integer = GetKeyValue("myvaluekey", "Integer")
But if I can't return the right type with a single function, then
> if I return "object", will the "dim x as string" at the start of the line convert the object to type string for me?
Or do I need to cast it as string as it comes out of GetValueKey
dim y as integer = CINT(GetKeyValue("myvaluek
Or do I need to call it with a separate function
dim y as integer GetKeyValueInteger("myvalu
Much to learn here, any further help would be appreciated.