Returning 2 values

  • Thread starter Thread starter Bob Hollness
  • Start date Start date
B

Bob Hollness

Hi all. I know this is basic but it is something that i have never learnt!

I have a function that returns a True or False to the Sub. But what i
actually want to do is return True or False and a number. How is this
possible?

Thanks
 
Bob said:
I have a function that returns a True or False to the Sub. But what i
actually want to do is return True or False and a number. How is this
possible?

There are (at least) two ways to do this.

Usually the simplest way is to return one of the values as the result of the
procedure, and allow the user to provide a variable as a parameter which
will receive the other value. For example, you could set your function up as
follows:

Public Function DoSomething(ByRef ReturnValue As Integer) As Boolean
ReturnValue = 10
Return True
End Function

When you call this, whichever variable you supply as the ReturnValue
parameter will be populated with the value 10 (because the variable was
passed By Reference instead of By Value). The boolean return value works as
normal.

Another way you could do this is by returning a structure or a class which
contains the two values. This is useful in some circumstances but for
something as simple as you appear to be doing, a ByRef variable is probably
the easiest solution to implement and use.

Hope that helps,
 
Use F# instead of VB and use a tuple.

Or create a Tuple Class and use that to encapsulate a boolean and integer
fields. Return this Tuple from your function.

public class Tuple
private _myBoolean as boolean
private _myNumber as integer
public property MyBoolean
get
return _myBoolean
end get
set(byval value as boolean)
_myBoolean = value
end set
end property
public property MyNumber
get
return _myNumber
end get
set(byval value as integer)
_myNumber = value
end set
end property
end class
 
sounds good but i thought that after the Return had been actioned, VB left
the function and went back to the calling one, thus not reaching the 2nd
Return.

??
 
Bob,

Something the same as Oenone, I like this more

\\\
sub main
dim myvalue as object
if dosomething(myvalue) then
messagebox.show(myvalue.tostring)
end if
end sub
private function dosomething(byval thisvalue as object) as boolean
thisvalue = "Hello"
return true
end function
///
just as alternative

Cor
 
Bob Hollness said:
I have a function that returns a True or False to the Sub. But what i
actually want to do is return True or False and a number. How is this
possible?

\\\
Public Structure Foo
Result As Boolean
Value As Integer
End Structure
..
..
..
Public Function Goo() As Foo
Dim f As Foo
f.Result = ...
f.Value = ...
Return f
End Function
///

- or -

\\\
Public Sub Goo(ByRef Result As Boolean, ByRef Value As Integer)
Result = ...
Value = ...
End Sub
..
..
..
Dim Value As Integer, Result As Boolean
Goo(Result, Value)
MsgBox(Result.ToString())
MsgBox(Value.ToString())
///
 
Hi Bob,

You walked in the trap, look again at the code from Oenone

:-)))

Cor
 
Bob Hollness said:
sounds good but i thought that after the Return had been actioned, VB left
the function and went back to the calling one, thus not reaching the 2nd
Return.
[...]
Public Function DoSomething(ByRef ReturnValue As Integer) As Boolean
ReturnValue = 10
Return True
End Function

Take a closer look... There is only one 'Return'...
 
Hi Bob,

Sounds like most of the suggestions will drive you to distraction. Simple:
one value is the return value of the function; the second? pass a reference
(byref) parameter:

callfunction(byval x as integer, byval y as string, byref comebacktome as
integer)

then change comebacktome as appropriate and you now have the 2 return
values.

HTH,

Bernie Yaeger
 
Since VB.NET is object oriented and you should be using OO principles,
minimizing the use of ByRef is one of those principles. Think abstraction
and encapsulation.

A function is supposed to be a black box: You put something in, you get
something out. OO principles as well as good software design principles say
to abstract away unnecessary side effects.

Once you use byRef you are creating a side effect on the Integer you pass
in.

It's much better to pass a struct or class in, and return the class or
struct out as Wagner suggested, than it is to use ByRef.


Bernie Yaeger said:
Hi Bob,

Sounds like most of the suggestions will drive you to distraction.
Simple: one value is the return value of the function; the second? pass a
reference (byref) parameter:

callfunction(byval x as integer, byval y as string, byref comebacktome as
integer)

then change comebacktome as appropriate and you now have the 2 return
values.

HTH,

Bernie Yaeger
 
Hi all. I know this is basic but it is something that i have never
learnt!

I have a function that returns a True or False to the Sub. But what i
actually want to do is return True or False and a number. How is this
possible?


You can return a structure or object with the values in it.
 
Hi Cor,

Why not simply use a byref variable?

Bernie Yaeger


Because it could change the original variable mistakenly.

For example, further in the code you expect the value to be 1, but
somewhere above you call the function and mess with the numbers.

Generally speaking, it's bad to use a variable like that to pass
information back and forth... unless I guess you have a dedicated variable
to do so. But in that case, you might as well use an object or structure.
 
Hi Zeno,

Technically I agree with you; practically, it's silly. We've been using
byref variables since the beginning of programming and we will for the
foreseeable future.

Bernie

Zeno Lee said:
Since VB.NET is object oriented and you should be using OO principles,
minimizing the use of ByRef is one of those principles. Think abstraction
and encapsulation.

A function is supposed to be a black box: You put something in, you get
something out. OO principles as well as good software design principles
say to abstract away unnecessary side effects.

Once you use byRef you are creating a side effect on the Integer you pass
in.

It's much better to pass a struct or class in, and return the class or
struct out as Wagner suggested, than it is to use ByRef.
 
Bernie,

Practically it makes more sense. It's not a technical issue.

The difference between between goto programming and procedural programming
is the abstraction layer that procedures provide for you. Instead of
thinking in terms of gotos, you think in terms of procedures.

http://pauillac.inria.fr/~xleroy/stuff/real-programmers

Read the diatribe against "Structured Programming"

The difference between procedural programming and OO programming is that you
have more ways of isolating complexities into their own black boxes and
presenting simple interfaces to the world.

If you decide to remain a procedural programmer in an OO language like
VB.NET than you are missing out on ways of making programming simpler and
more enjoyable.
 
Zeno,

This is not 'goto' programming. If you don't like an idea, don't find the
'reprehensible' reference to diminish it.

Bernie
 
Zeno,

I have seen this kind of messages yesterday more, when someone put something
in discussion about practise of OOP, does that than directly mean that he
makes "goto" programs?

I find this not the way to react to Bernie.

Just my thought,

Cor
 
Herfried said:
Take a closer look... There is only one 'Return'...

Hmm, sorry about that, I could have named the parameter variable slightly
more clearly. :/
 
Lucas Tam said:
You can return a structure or object with the values in it.


Boy, my head is spinning further! I can't quite get my head around the
ByRef method though. How does it fit to the example below? E.g. If False
is returned, I also want to return the value of the variable CookieLevel to
show why it was returned False.

Function RequestNumberOfCookiesInJar

Dim YesNo as Boolean
Dim Requested as Integer
Requested = 10
YesNo = CheckMyValue(Requested)

If YesNo = True Then MsgBox("Delicious!")

End Function


Function CheckRemainingCookies(HowManyYouWant as Integer)

Dim CookieLevel as Integer

CookieLevel = 5

If HowManyYouWant < CookieLevel Then
Return Tre
Else
Return False
End If

End Function



thanks all for all the help!

p.s. obviously the above is not what i want to do, so excuse any errors!
 

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

Back
Top