473,473 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

"Call" function ?


I have a function named

getID3info (lvwDiscInfo.SelectedItem).

What is the difference between

getID3info (lvwDiscInfo.SelectedItem)
and
Call getID3info(lvwDiscInfo.SelectedItem) ?

They both work properly - is there some kind of runtime overhead
difference between the two ?

Thanks.
¤ Alias
Jul 17 '05 #1
3 46944
On Thu, 03 Jul 2003 04:34:37 GMT, ¤ Alias <-@-.> wrote:
[ oops....here is the correct function name]


I have a function named

Public Function getID3info(ByRef x As String) As Integer

What is the difference between

getID3info (lvwDiscInfo.SelectedItem)
and
Call getID3info(lvwDiscInfo.SelectedItem) ?

They both work properly - is there some kind of runtime overhead
difference between the two ?

Thanks.
¤ Alias

Jul 17 '05 #2
> I have a function named

Public Function getID3info(ByRef x As String) As Integer

What is the difference between

getID3info (lvwDiscInfo.SelectedItem)
and
Call getID3info(lvwDiscInfo.SelectedItem) ?

They both work properly - is there some kind of runtime overhead
difference between the two ?


First off, VB has two types of procedures, a Sub and a Function. A Sub
simply executes codes, a Function does the same thing; but, in addition, it
also returns a value. You declared your procedure as a Function, but the two
methods you ask your question about do not show any value being accepted by
the calling routine. Given that, I'd suggest you declare your sample
procedure as a Sub instead. A Function can be used as a Sub, but you are
wasting the memory location that is supposed to hold the return value.

Now, as to your question... whenever you use parentheses around something
that is not part of the syntax, VB evaluates the expression contained inside
(even if there is nothing to "calculate"). The parentheses in the line with
the Call keyword are part of the syntax, so the (assumed) Sub receives the
arguments ByRef as declared. However, in the other statement, the
parentheses are **not** part of the syntax (see the Remarks section of the
Help files for the Call statement). That means VB evaluates the single
expression it finds enclosed within. It does that by establishing a
temporary memory location in which to carry at the evaluation and passes a
pointer to the evaluated contents at that memory address. This means the Sub
has access to the actual argument being passed by the Call statement and can
modify its value in the calling code's procedure; in the other statement,
the Sub has no access to the argument at all and cannot affect its value
back in the calling procedure. I'd say the reason both expressions work
properly is because you are not changing the value of the argument within
the Sub.

A quick example...

Private Sub Form_Load()
Dim SomeInteger As Integer
SomeInteger = 1
Debug.Print SomeInteger & " <== original value"
AddFive (SomeInteger)
Debug.Print SomeInteger & " <== didn't change"
Call AddFive(SomeInteger)
Debug.Print SomeInteger & " <== this time, it changed"
AddFive SomeInteger
Debug.Print SomeInteger & " <== no parens, it changed again"
End Sub

Sub AddFive(NumberIn As Integer)
NumberIn = NumberIn + 5
End Sub

Start a new project and paste the above code into the form's code window;
Run the program.

The misuse of parentheses can also generate errors when dealing with
objects. Start a new project and add a TextBox and two(2) CommandButtons and
paste this code into the form's code window

Private Sub Command1_Click()
Call DeleteText(Text1)
End Sub

Private Sub Command2_Click()
DeleteText (Text1)
End Sub

Sub DeleteText(TBox As TextBox)
TBox.Text = ""
End Sub

Run the project, type some text into the TextBox and click Command1. The
text is deleted as expected. Now type some more text into the TextBox and
click Command2. Whoops... an error. Why? Because when you put the
parentheses around that Text1 object, but did not use the Call keyword, you
forced VB to evaluate the Text1 object. TextBoxes have a default property of
Text (the characters making up the content of the TextBox) and it was a
pointer to the temporary memory address VB copied the contents of Text1 to
that was passed. Since this is a String, and not a TextBox object, VB
generated a Type Mismatch error.

Hope the above helped.

Rick - MVP
Jul 17 '05 #3
On Thu, 3 Jul 2003 01:42:16 -0400, "Rick Rothstein"
<ri************@NOSPAMcomcast.net> wrote:

Rick,
Thanks for the detailed reply. More responses in the copy below.

I have a function named

Public Function getID3info(ByRef x As String) As Integer

What is the difference between

getID3info (lvwDiscInfo.SelectedItem)
and
Call getID3info(lvwDiscInfo.SelectedItem) ?

They both work properly - is there some kind of runtime overhead
difference between the two ?
First off, VB has two types of procedures, a Sub and a Function. A Sub
simply executes codes, a Function does the same thing; but, in addition, it
also returns a value. You declared your procedure as a Function, but the two
methods you ask your question about do not show any value being accepted by
the calling routine. Given that, I'd suggest you declare your sample
procedure as a Sub instead. A Function can be used as a Sub, but you are
wasting the memory location that is supposed to hold the return value.


Yes, you are right. I'm revisiting some code I wrote when I was an
absolute beginner - the function (which reads ID3 info from an MP3)
was probably a cut and paste from some web sample code.

I've changed it to a Sub, since, as you point out, no value is
returned. I've also modified the syntax as advised.
Now, as to your question... whenever you use parentheses around something
that is not part of the syntax, VB evaluates the expression contained inside
(even if there is nothing to "calculate"). The parentheses in the line with
the Call keyword are part of the syntax, so the (assumed) Sub receives the
arguments ByRef as declared. However, in the other statement, the
parentheses are **not** part of the syntax (see the Remarks section of the
Help files for the Call statement). That means VB evaluates the single
expression it finds enclosed within. It does that by establishing a
temporary memory location in which to carry at the evaluation and passes a
pointer to the evaluated contents at that memory address. This means the Sub
has access to the actual argument being passed by the Call statement and can
modify its value in the calling code's procedure; in the other statement,
the Sub has no access to the argument at all and cannot affect its value
back in the calling procedure. I'd say the reason both expressions work
properly is because you are not changing the value of the argument within
the Sub.
Ok - understood.
A quick example...

Private Sub Form_Load()
Dim SomeInteger As Integer
SomeInteger = 1
Debug.Print SomeInteger & " <== original value"
AddFive (SomeInteger)
Debug.Print SomeInteger & " <== didn't change"
Call AddFive(SomeInteger)
Debug.Print SomeInteger & " <== this time, it changed"
AddFive SomeInteger
Debug.Print SomeInteger & " <== no parens, it changed again"
End Sub

Sub AddFive(NumberIn As Integer)
NumberIn = NumberIn + 5
End Sub

Start a new project and paste the above code into the form's code window;
Run the program.
I see. Interesting.
The misuse of parentheses can also generate errors when dealing with
objects. Start a new project and add a TextBox and two(2) CommandButtons and
paste this code into the form's code window

Private Sub Command1_Click()
Call DeleteText(Text1)
End Sub

Private Sub Command2_Click()
DeleteText (Text1)
End Sub

Sub DeleteText(TBox As TextBox)
TBox.Text = ""
End Sub

Run the project, type some text into the TextBox and click Command1. The
text is deleted as expected. Now type some more text into the TextBox and
click Command2. Whoops... an error. Why? Because when you put the
parentheses around that Text1 object, but did not use the Call keyword, you
forced VB to evaluate the Text1 object. TextBoxes have a default property of
Text (the characters making up the content of the TextBox) and it was a
pointer to the temporary memory address VB copied the contents of Text1 to
that was passed. Since this is a String, and not a TextBox object, VB
generated a Type Mismatch error.
Got it.
I went ahead and tested option 3:

Private Sub Command2_Click()
DeleteText Text1
End Sub

and sure enough, it cleared the textbox.
Hope the above helped.
Very much so - I'm glad I asked.
Rick - MVP
Thanks
¤ Alias


Jul 17 '05 #4

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

Similar topics

2
by: Sylwia | last post by:
Hi! I need your help... I have the following problem. I've implemented the python Windows Service which behaves like a log supervisor. If the space used by log files is bigger than a given...
7
by: Vaca Louca | last post by:
Hello, My setup: Debian sarge on dual Pentium 4. g++ 3.3.5-3. (the other system is Windows XP with MS Visual Studio .NET 2003) I have an auto_array<T> template (based on a template taken from...
2
by: seberino | last post by:
I'm trying run a homegrown profiler on some Python code. Rather than apply profiler wrapper to ALL functions by hand.... Is there a low level Python function I can override to modify how ALL...
15
by: Steve Jorgensen | last post by:
I'm looking for coding style opinions. It looks to me like most VB/VBA coders use the command-style syntax for calling subs or functions being treated as subs in a particular context ... Foo...
13
by: Larry Menard | last post by:
Test code: $dbconn = odbc_connect($dbname, $username, $password); $path = "C:\Temp\myJar.jar"; $statement = "CALL SQLJ.INSTALL_JAR('file://$path', 'myJarId')"; $result = odbc_exec($dbconn,...
10
by: Thorsten Ottosen | last post by:
Hi, I'm trying to escape html before its saved in a database. I tried $text = htmlentities( $reader->value ); but that don't work for e.g. japanese characters.
5
by: Frederick Gotham | last post by:
If we have a simple class such as follows: #include <string> struct MyStruct { std::string member; MyStruct(unsigned const i) {
2
by: John M. Gamble | last post by:
I'm getting this message in Visual Studio 2005: PInvokeStackImbalance was detected Message: A call to PInvoke function 'Refresh!Refresh.Main::WNetAddConnection2' has unbalanced the stack. ...
2
by: r_ahimsa_m | last post by:
Hello, I am learning PHP5. I need to parse XML file and I found a solution in some book on PHP5 ("PHP5 Programming" by Rasmus Lerdors and others). Unfortunately I have two problems that I don't...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.