Re: Basic Newbie Question: What's the ":=" operator for?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



termiflyer wrote:
> See if I interpret this correctly. These are the same:
>
> oDoc.Protect _
> Type:=wdAllowOnlyFormFields, _
> NoReset:=True
>
> and
>
> oDoc.Protect(wdAllowOnlyFormFields, True)
>
> Using named parameters only makes the code more readable, nothing
> else?

Not quite... there's one more wrinkle in there.

If you omit the parameter names, then you must have at least a comma for
every possible parameter up to the one just before the last value you
supply, and you have to put the values in the order specified by the syntax
diagram in the help. If you use the names, then you can include only the
ones you're actually giving values for, in any order.

As an example, take the MsgBox function. The full syntax is

MsgBox(prompt[, buttons] [, title] [, helpfile, context])

Let's say I want to supply values for the prompt and title parameters, and
accept the defaults for buttons, helpfile, and context. If I supply
parameters by position (that is, without the names), then I have to write

MsgBox("Hello", , "My Title")

Notice the empty second comma, which serves as a placeholder for the buttons
parameter.

If I use the names, then I can put in just the prompt and title without
worrying about any of the default values, and I can even supply the values
out of order:

MsgBox(title:="My Title", prompt:="Hello")

One other thing: Use the parentheses around the parameters only if you're
going to use the return value of the function; if you're using it as a
statement, omit the parentheses. See
http://word.mvps.org/FAQs/MacrosVBA/BracketsWithArgmnts.htm for explanation.

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org


.



Relevant Pages

  • Re: English-like Python
    ... interpretation of a typical statement without its parentheses. ... CurrentTime) ... But even if RB doesn't have these things, I question that the syntax is ... "beautiful". ...
    (comp.lang.python)
  • Re: Casting the return value of malloc()...
    ... Most arguments to sizeof do not require parentheses. ... Why should it need any further name than "part of the syntax?" ... Depending on context, it may specify an operation to be ... See, punctuators can be operators, but the set of operators is broader. ...
    (comp.lang.c)
  • Re: Chinese Syntax
    ... the parentheses can be omitted when such omission will not lead to ambiguity. ... > One of the biggest flaws in C syntax, in my opinion, is the required> parentheses for test conditions. ... > Python uses such a construct for test conditions. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: What is a null pointer constant?
    ... > NULL] thus NULL would act as an object macro yet #NULL would be "(void ... I would argue that a null pointer constant surrounded by ... > parentheses is still valid for initialization on the grounds that ... It's determined entirely by the syntax of the expression, ...
    (comp.std.c)
  • Re: Chinese Syntax
    ... the parentheses can be omitted when such omission will not lead to ambiguity. ... > One of the biggest flaws in C syntax, in my opinion, is the required> parentheses for test conditions. ... > Python uses such a construct for test conditions. ...
    (comp.lang.c)