473,396 Members | 1,914 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Function with Two Values

Good evening all,

I was wondering if there is a way to do a function or different sub
routine that returns two or more values.

I usually use a function to return a boolean value, however, this
sometimes requires me to undertake two functions. I would like to do it
in one operation.

I have thought that I could get the function to reutrn a string, then
simply break-up the string into seperate chunks that mean different
things, but I thought I would ask to see if there is a neater way to do
this.

As an example I would like to return a boolean and an integer from a
function, i.e:

Private Sub Query()

Dim Answer, Answer_Part_1, Answer_Part_2 as String
Answer = Working_Out(Blah, Blah, Blah)
Answer_Part_1 = Left(Answer,3)
Answer_Part_2 = Right(Answer,3)

End Sub

Private Function Working_Out(ByVal Blah1 as String,ByVal Blah2 as
String,ByVal Blah3 as String) as String

Dim Return_String as String
Return_String = "Yes" & 123

Working_Out = Return_String

End Function

Any assistance you may be able to provide would be greatly appreciated.

Yours sincerely,

William Foster

*** Sent via Developersdex http://www.developersdex.com ***
Dec 4 '06 #1
6 971
You could pass them byref....
"William Foster" <no****@devdex.comwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
Good evening all,

I was wondering if there is a way to do a function or different sub
routine that returns two or more values.

I usually use a function to return a boolean value, however, this
sometimes requires me to undertake two functions. I would like to do it
in one operation.

I have thought that I could get the function to reutrn a string, then
simply break-up the string into seperate chunks that mean different
things, but I thought I would ask to see if there is a neater way to do
this.

As an example I would like to return a boolean and an integer from a
function, i.e:

Private Sub Query()

Dim Answer, Answer_Part_1, Answer_Part_2 as String
Answer = Working_Out(Blah, Blah, Blah)
Answer_Part_1 = Left(Answer,3)
Answer_Part_2 = Right(Answer,3)

End Sub

Private Function Working_Out(ByVal Blah1 as String,ByVal Blah2 as
String,ByVal Blah3 as String) as String

Dim Return_String as String
Return_String = "Yes" & 123

Working_Out = Return_String

End Function

Any assistance you may be able to provide would be greatly appreciated.

Yours sincerely,

William Foster

*** Sent via Developersdex http://www.developersdex.com ***

Dec 4 '06 #2
There are a number of ways to achieve this.

One way is to use an array to as the return value but this would require
that the type of each element be the same, so as a variation you could use
an Arraylist:

Private Sub Query()

Dim Answer As Arraylist = Working_Out(Blah, Blah, Blah)

Dim Answer_Part_1 As Boolean = CType(Answer(0), Boolean)

Dim Answer_Part_2 As String = CType(Answer(1), String)

End Sub

Private Function Working_Out(ByVal Blah1 as String,ByVal Blah2 as
String,ByVal Blah3 as String) as Arraylist

Dim Answer As New Arraylist

Answer.Add(True)

Answer.Add("123")

Return Answer

End Function
"William Foster" <no****@devdex.comwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
Good evening all,

I was wondering if there is a way to do a function or different sub
routine that returns two or more values.

I usually use a function to return a boolean value, however, this
sometimes requires me to undertake two functions. I would like to do it
in one operation.

I have thought that I could get the function to reutrn a string, then
simply break-up the string into seperate chunks that mean different
things, but I thought I would ask to see if there is a neater way to do
this.

As an example I would like to return a boolean and an integer from a
function, i.e:

Private Sub Query()

Dim Answer, Answer_Part_1, Answer_Part_2 as String
Answer = Working_Out(Blah, Blah, Blah)
Answer_Part_1 = Left(Answer,3)
Answer_Part_2 = Right(Answer,3)

End Sub

Private Function Working_Out(ByVal Blah1 as String,ByVal Blah2 as
String,ByVal Blah3 as String) as String

Dim Return_String as String
Return_String = "Yes" & 123

Working_Out = Return_String

End Function

Any assistance you may be able to provide would be greatly appreciated.

Yours sincerely,

William Foster

*** Sent via Developersdex http://www.developersdex.com ***

Dec 4 '06 #3
guy
return a structure containing a boolean and the other value that you need to
return

hth

guy

"William Foster" wrote:
Good evening all,

I was wondering if there is a way to do a function or different sub
routine that returns two or more values.

I usually use a function to return a boolean value, however, this
sometimes requires me to undertake two functions. I would like to do it
in one operation.

I have thought that I could get the function to reutrn a string, then
simply break-up the string into seperate chunks that mean different
things, but I thought I would ask to see if there is a neater way to do
this.

As an example I would like to return a boolean and an integer from a
function, i.e:

Private Sub Query()

Dim Answer, Answer_Part_1, Answer_Part_2 as String
Answer = Working_Out(Blah, Blah, Blah)
Answer_Part_1 = Left(Answer,3)
Answer_Part_2 = Right(Answer,3)

End Sub

Private Function Working_Out(ByVal Blah1 as String,ByVal Blah2 as
String,ByVal Blah3 as String) as String

Dim Return_String as String
Return_String = "Yes" & 123

Working_Out = Return_String

End Function

Any assistance you may be able to provide would be greatly appreciated.

Yours sincerely,

William Foster

*** Sent via Developersdex http://www.developersdex.com ***
Dec 4 '06 #4
William: Think "object oriented". Never set yourself up with additional
work by (among other things) concatenating return values requiring you to
immediately break them apart again.

As was pointed out (and is often done) you can pass the variables Answer_1
and Answer_2 by reference to the function which will assign them (leaving
the return value available for some other use.) Better yet though is Guy's
reply. If you "think OOP" (and look at the .Net framework (and other
frameworks for that matter)) you will see they often return "objects" or as
Guy is pointing out a structure (sort of a lightweight object.) They can be
expanded (if you need a 3rd or 4th value) and they are self-documenting as
the "parts" you refer to would be named something like Answer.Text and
Answer.Number.

Tom
"William Foster" <no****@devdex.comwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
Good evening all,

I was wondering if there is a way to do a function or different sub
routine that returns two or more values.

I usually use a function to return a boolean value, however, this
sometimes requires me to undertake two functions. I would like to do it
in one operation.

I have thought that I could get the function to reutrn a string, then
simply break-up the string into seperate chunks that mean different
things, but I thought I would ask to see if there is a neater way to do
this.

As an example I would like to return a boolean and an integer from a
function, i.e:

Private Sub Query()

Dim Answer, Answer_Part_1, Answer_Part_2 as String
Answer = Working_Out(Blah, Blah, Blah)
Answer_Part_1 = Left(Answer,3)
Answer_Part_2 = Right(Answer,3)

End Sub

Private Function Working_Out(ByVal Blah1 as String,ByVal Blah2 as
String,ByVal Blah3 as String) as String

Dim Return_String as String
Return_String = "Yes" & 123

Working_Out = Return_String

End Function

Any assistance you may be able to provide would be greatly appreciated.

Yours sincerely,

William Foster

*** Sent via Developersdex http://www.developersdex.com ***

Dec 4 '06 #5
William,

I don't like it, but to make this thread complete

Private Sub MySub(byval mybool as boolean, byval myint as int)
mybool = true
myInteger = 2
End sub

mybool as booleand = false
myInteg as int = 1
mySub(mybool,mystring)

The bool is now true and the myInt = 2,

But it is confusing to use, therefore I don't like it.

I like more the method with the objects or arrays as is told here as well.

Cor

"William Foster" <no****@devdex.comschreef in bericht
news:%2****************@TK2MSFTNGP05.phx.gbl...
Good evening all,

I was wondering if there is a way to do a function or different sub
routine that returns two or more values.

I usually use a function to return a boolean value, however, this
sometimes requires me to undertake two functions. I would like to do it
in one operation.

I have thought that I could get the function to reutrn a string, then
simply break-up the string into seperate chunks that mean different
things, but I thought I would ask to see if there is a neater way to do
this.

As an example I would like to return a boolean and an integer from a
function, i.e:

Private Sub Query()

Dim Answer, Answer_Part_1, Answer_Part_2 as String
Answer = Working_Out(Blah, Blah, Blah)
Answer_Part_1 = Left(Answer,3)
Answer_Part_2 = Right(Answer,3)

End Sub

Private Function Working_Out(ByVal Blah1 as String,ByVal Blah2 as
String,ByVal Blah3 as String) as String

Dim Return_String as String
Return_String = "Yes" & 123

Working_Out = Return_String

End Function

Any assistance you may be able to provide would be greatly appreciated.

Yours sincerely,

William Foster

*** Sent via Developersdex http://www.developersdex.com ***

Dec 4 '06 #6
Thank you all for your inpt on a beginner question, I will be
investigating the Structure and Answer as an Array List options when I
get home tonight.

Yours sincerely,

William Foster

*** Sent via Developersdex http://www.developersdex.com ***
Dec 4 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my...
2
by: csx | last post by:
Hi all, Here's a simple problem I'm having problems with. How do I pass an array element to a function. For instance, values = new values(1, 1,'A',0); values = new values(2, 2,'B',1);...
3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
10
by: Mamuninfo | last post by:
Hello, Have any function in the DB2 database that can generate unique id for each string like oracle, mysql,sybase,sqlserver database. In mysql:- select md5(concat_ws("Row name")) from...
13
by: Maroon | last post by:
Hi, I want to write a standard java user defined function, like this example:- select db_fun("roll"), roll from student; **db_fun() will work for all tables of the all databse here db_fun is...
19
by: Gary Kahrau | last post by:
I want to create a function and can only get half way to my goal. dim sStr as string sStr = Entry(2,"a,b,c,d") ' Sets sStr = "b" Entry(2,sStr) = "B" ' Sets sStr = "a,B,c,d"...
28
by: Larax | last post by:
Best explanation of my question will be an example, look below at this simple function: function SetEventHandler(element) { // some operations on element element.onclick = function(event) {
7
by: Darko | last post by:
Hello, I have this particular problem with eval() when using Microsoft Internet Explorer, when trying to define an event handler. This is the code: function BigObject() { this.items = new...
160
by: DiAvOl | last post by:
Hello everyone, Please take a look at the following code: #include <stdio.h> typedef struct person { char name; int age; } Person;
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.