Solved

Return Multiple Types from Function

Posted on 2006-07-13
8
177 Views
Last Modified: 2012-05-05
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!
0
Comment
Question by:codequest
8 Comments
 
LVL 17

Accepted Solution

by:
ZeonFlash earned 168 total points
ID: 17104739
The simple answer would be to set the return value as an Object, so:

Function GetKeyValue(Key(s), DataType) as Object
.
.
.
End Function

The bad part about that is that you won't know what type you're sending out, so by adding the datatype as a Referenced parameter, you can pass the return value type back.  Here's an example:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim ReturnType As Type
        Dim ReturnObject As Object

        ReturnObject = GetKeyValue(1, ReturnType)

        Select Case ReturnType.Name
            Case "Single"
                MsgBox("Single")
            Case "Integer"
                MsgBox("Int32")
            Case "String"
                MsgBox("String")
        End Select
End Sub

Private Function GetKeyValue(ByVal myInt As Integer, ByRef ReturnType As Type) As Object
        ReturnType = GetType(System.String)
        ReturnType = GetType(System.Single)
        ReturnType = GetType(Integer)
        Return 5.2
End Function

Note that I just created a random parameter for the GetKeyValue function.  Fit it to whatever your function requires.
0
 
LVL 2

Author Comment

by:codequest
ID: 17105169
Thanks for the input.   Hmmm...You may have illustrated what to do, but I'm not quite getting it...

At a higher level, what I'm trying to do is get control values from key/value pairs stored in the DB.  
http://www.experts-exchange.com/Databases/Microsoft_SQL_Server/Q_21918874.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("myvaluekey","Integer"))   and just return an object, knowing that it IS an integer?

Or do I need to call it with a separate function

dim y as integer GetKeyValueInteger("myvaluekey","Integer")

Much to learn here, any further help would be appreciated.
0
 
LVL 39

Assisted Solution

by:appari
appari earned 166 total points
ID: 17105198
if you are using .net 2.0
 may be you can try to use Generics.

for more information on Generics check these links on MSDN
http://msdn2.microsoft.com/en-us/library/79btc5zc.aspx
http://msdn2.microsoft.com/en-us/library/w256ka79.aspx
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 39

Expert Comment

by:appari
ID: 17105234
if you are using .net 2.0 and if you want to try generic classes post your existing code here.
0
 
LVL 2

Author Comment

by:codequest
ID: 17105776
Thanks for inputs.  Lemme do the prelim read on generics and I'll get back here.  
0
 
LVL 10

Assisted Solution

by:gangwisch
gangwisch earned 166 total points
ID: 17105887
structure mystruct
dim i as integer
dim s as string
end structure

function test(myint as integer,mystr as string) as mystruct
dim st as mystruct
st.i = myint
st.s = mystr
return st
end function
0
 
LVL 2

Author Comment

by:codequest
ID: 17109602
gangwisch:  Thanks for input.  That's an interesting alternative...seems like pretty compact coding, too.
0
 
LVL 2

Author Comment

by:codequest
ID: 17141204
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!
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now