+ Reply to Thread
Results 1 to 6 of 6

Executing methods - parentheses or no parentheses?

  1. #1
    Registered User
    Join Date
    07-10-2013
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    77

    Executing methods - parentheses or no parentheses?

    I've been thinking about how some methods require parentheses while others don't and decided to look into why that was the cause. MSDN said that if the method returns a value AND you're not ignoring the returned value by forgoing a variable on the left hand side, then parentheses are necessary. However, even if the method has the capability of returning a value, you can remove the parentheses if you don't care what the value is.

    Ex:

    This would work
    Please Login or Register  to view this content.
    This would also work (although it wouldn't be very useful since you have no way of knowing how the user responded)
    Please Login or Register  to view this content.
    This would NOT work (method is returning a value but there are no parentheses)
    Please Login or Register  to view this content.
    This would give you a syntax error (parentheses indicates a returned value is expected but there is no variable for it
    Please Login or Register  to view this content.

    However, I found something I don't understand about this. I was using the attachments.add method (which is what started it all), and Excel accepts it with or without parentheses, regardless of whether or not you have a variable.

    Ex:

    This works even though there are parentheses and no variable

    Please Login or Register  to view this content.
    According to the MSDN, this method returns an attachment object. Even more confusing is that it will accept a variable with or without the "Set" statement even though it is an object.

    Ex:

    This works
    Please Login or Register  to view this content.
    And this works
    Please Login or Register  to view this content.

    When I hover over "outm" with my mouse, it shows as a string what the pathname is. Which I also don't get because usually nothing shows up for objects.


    Does anyone have any insight?

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Executing methods - parentheses or no parentheses?

    When you invoke a Sub with Call, you must use parens around the arguments:

    Please Login or Register  to view this content.
    When you assign the result of a function to a variable, you must use parens around the arguments:

    Please Login or Register  to view this content.
    If you want to pass arguments by value or by reference, according to the procedure's signature, you don't include parens:

    Please Login or Register  to view this content.
    If you want to pass arguments ByVal, irrespective of the procedure’s signature (which won't work for a procedure expecting an array (because arrays are always passed by reference) or an object), you enclose the argument in parens, or an extra set of parens. In the examples below, arg1 is passed by value, and arg2 according to the procedure’s signature:
    Please Login or Register  to view this content.
    Objects are always passed ByRef, so you can never put unnecessary parens around an object variable. Chip Pearson (http://www.cpearson.com/excel/byrefbyval.aspx) explains the meaning of ByRef and ByVal for an object variable:

    The ByRef and ByVal modifers indicate how the reference is passed to the called procedure. When you pass an object type variable to a procedure, the reference or address to the object is passed -- you never really pass the object itself. When you pass an object ByRef, the reference is passed by reference and the called procedure can change the object to which that reference refers to. When an object is passed ByVal, a copy of the reference (address) of the object is passed.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,696

    Re: Executing methods - parentheses or no parentheses?

    Methods are procedures. There are two types of procedures, and therefore two types of methods. The rules below apply

    A Sub has 0 or more parameters.
    A Function has 0 or more parameters, and also returns a value.

    If you are calling a Sub just by its name, then the parentheses around parameters must be omitted. However, if you use the Call statement, then the parentheses must be included.

    Please Login or Register  to view this content.
    If you wish to call a Function and not capture the return value, you may call it using the same rules as for calling a Sub.

    Please Login or Register  to view this content.
    I cannot explain why you are able to call the method Add as a Sub without the Call statement and still include the parentheses.

    When a Function method returns an object, you may assign that object to a variable (if the object is defined and there is no type conflict in doing so). Objects have a default attribute, which is what will be assigned to the variable. For example, the default attribute of a Range object is the Value attribute.

    Please Login or Register  to view this content.
    After this code executes, range R2 will have a copy of the value that is in R1. R1 still refers to cell A1, and R2 still refers to cell B2, but now cell B2 has a copy of the value from A1.

    But when you use Set, it means that you are causing the object variable to point to the same object as the one you are setting it to. It is much different than using assignment without Set.
    Please Login or Register  to view this content.
    After this code executes, R2 and R1 both refer to cell A1.

    I hope this takes some of the mystery out.
    Last edited by 6StringJazzer; 04-08-2015 at 01:04 PM. Reason: code for Function example was corrected
    Jeff
    | | |·| |·| |·| |·| | |:| | |·| |·|
    Read the rules
    Use code tags to [code]enclose your code![/code]

  4. #4
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Executing methods - parentheses or no parentheses?

    I cannot explain why you are able to call the method Add as a Sub without the Call statement and still include the parentheses.
    When you add parens around an argument that doesn't require them, the argument is evaluated, and the result of the evaluation is passed to the procedure. It means the argument is passed by value regardless of how the procedure requested it.

    That also explains why you can't use parens around two or more arguments when they are not required; the evaluation of (arg1) may be meaningful, but the evaluation of (arg1, arg2) is not. That's why this generates an error:

    Please Login or Register  to view this content.
    That also explains why if you call a procedure expecting a range with parens where none are required, you get a type mismatch error; because the evaluation of a range is a Variant, or Variant/Array.

    Parens means completely different things according to context. VBA might have used some other bookends (curly braces, square brackets, whatever) to indicate enclosing a list versus a call for evaluation, but it didn't.
    Last edited by shg; 03-23-2015 at 03:51 PM.

  5. #5
    Registered User
    Join Date
    07-10-2013
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    77

    Re: Executing methods - parentheses or no parentheses?

    That was very interesting, I had no idea there were so many syntaxes for this. I'm also interested to learn that you can override a byref argument - who knew?!

    Thanks!

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Executing methods - parentheses or no parentheses?

    You're welcome.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] .End and then parentheses
    By freshfruit in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-05-2014, 09:31 PM
  2. [SOLVED] Extracting all text from parentheses (including parentheses)
    By j4ke101 in forum Excel Formulas & Functions
    Replies: 12
    Last Post: 05-10-2013, 08:03 AM
  3. Replies: 6
    Last Post: 01-13-2011, 03:00 AM
  4. How to add parentheses...
    By hello_lpc in forum Excel General
    Replies: 4
    Last Post: 11-10-2008, 03:02 AM
  5. parentheses
    By Brendy in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 01-15-2006, 05:25 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1