About Methods’ Syntax:
I heard about two syntaxes of parameter declaraton. I use the first one for MulT and the other one for MulV:
$MathObject = (New-Module {
function MulT([int]$Factor1, [int]$Factor2) {
$Factor1 * $Factor2
}
function MulV{
param(
[int]$Factor1,
[int]$Factor2
)
return $a * $b
}
} -AsCustomObject)
What’s the difference between the syntax of MulT and the syntax of MulV? Which should I use?
Named Arguments:
Cmdlets can have named arguments:
Multiply-IntegerFactors -Factor1 5 -Factor2 8
Can methods of objects have named arguments too? I’m thinking about something like:
$MathObject.MulT(-Factor1 5, -Factor2 8)
EDIT: Do you think that it’s a good solution to use a hash table as argument in order to store the named arguments there?: $MathObject.MulT(@{Faktor1=5, Faktor2=8}) – Or is there any better solution?
Conventions On Methods:
There are strict naming conventions on Cmdlets (Verb-CamelCaseNoun) and modules (CamelCaseModuleName). Are there any (naming) conventions on methods too?
Thanks.
The difference in how the parameters are declared will make no differences in these 2 functions. In general, you have more flexibility to modify parameters using attributes if you use the param() syntax. There does seem to be an issue with the names of the parameters and how they are referenced in the body of the MulV function ($factor1/2 and $a/$b).
As far as using named parameters for methods, that doesn’t work.