Using sub with byref Param or Function return value

  • Thread starter Thread starter Ghislain Tanguay
  • Start date Start date
G

Ghislain Tanguay

Hi! I'm maybe from old school but, for me, whebn it's time to call a method
from DCOM, remoting or web service returning a
string or anything else I use a function. It's more verbose.

In my new company, much of my coworker works with Public sub ( byval Itemx
as integer, byref returnCode as Integer).

I don't understand why they use this kind of sub and i'm wondering if this
kind of sub take more time to response?

So, my question is, I have to debate this point tomorrow PM, is there any
good raison why we should prefer function to sub with a byref when byref
variable only seve to return a value?

Tks
 
Hi,

The biggest thing is that if you are using a web service you can't call
ByRef. So the returnCode would never be valid in those situations. Also I
don't return error codes. An error is caught in the object and then either
handled or thrown back up to the calling routine. Returning error codes
forces you to test for errors at every single line of code that makes the
call. Using a centralized Error Handling system will make life a lot
easier. Good luck! Ken.
 
It is clearer as to what the intent of a function is, vs that of a sub with
a byref parameter. I have no idea why anyone would make it so difficult to
get a return value by using byref parameters with a sub, instead of
returning the result in a function.

Also, using subs you can't nest function calls - you have to have a separate
line of code for each function call, since you need to pass in the variable,
instead of just the result of a function call.
 
Both techniques have their own uses. You would a pass parameter ByRef if you
already have some value in it and want the procedure to return a modified
value. Another place where you would use ByRef variables is if you want to
return multiple values - since a function can return only one value, one
would usually change it to a sub and pass in the parameters as ByRef so you
have sort of 'multiple return values'. For most other purposes, ByVal would
be the way to go. Also, performance is not really an issue between the two,
AFAIK. So, that should definitely not be a criteria for using one over the
other. Another point to note is that we are talking about value types here.
For reference types, ByVal and ByRef behave differently.

http://msdn.microsoft.com/library/d.../vbcn7/html/vaconargumentpassingmechanism.asp


hope that helps..
Imran.
 
Tks Ken,

Acutally I use a Try catch Error and throw a new SoapError.

But the point is, they use byref in webservice and it's ok too! There is no
problem using byref parameter with web service. The wsdl created will
transform those parameters
for an inout parameter.
 
For returning multiples values, I usually return an object, so my function
can do the job.
 

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