English 中文(简体)
职能司(“/”)
原标题:Division ("/") in function

我不想写司的简单职能,但我有错误。

PS C:Usersjohn> Function Div($x, $y) { $x / $y }
PS C:Usersjohn> Div (1, 1)
Method invocation failed because [System.Object[]] doesn t contain a method named  op_Division .
At line:1 char:28
+ Function Div($x, $y) { $x / <<<<  $y }
    + CategoryInfo          : InvalidOperation: (op_Division:String) [], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

What is my mistake? Thanks

最佳回答

你错误地援引这一职能。 职能援引权:

Div 1 1

而(1,1)是反对。

如果你想要防止使用错误,则宣布职能为:

Function Div([Parameter(Mandatory=$true)][double]$x, [Parameter(Mandatory=$true)][double]$y) { $x / $y }

[参数(强制性=美元)]确保这两种数值都得到确认。 司总是在权力分配方面进行双重划分,即使有愤怒,但执行型号[双重]却赢得了固定的惯用,并确保投入类型是你所期望的。

问题回答

You should cast the arguments of the division operator to ints in your function body, otherwise they will be treated as strings (even if they look like ints), and strings don t support the / operator:

<代码>[int]x / [int]美元





相关问题
Mutually exclusive powershell parameters

SCENARIO I m writing a cmdlet for Powershell 2.0 using Visual Studio 2008 and .NET 3.5 the cmdlet requires 3 arguments. my intended grammar of the cmdlet is something like this: cmdletname [foo|...

Run a program from PowerShell with timeout

I ll write a script that runs a program and wait for it finished. But if the program is not finished within a specified time I want that the program is killed.

How to transpose data in powershell

I have a file that looks like this: a,1 b,2 c,3 a,4 b,5 c,6 (...repeat 1,000s of lines) How can I transpose it into this? a,b,c 1,2,3 4,5,6 Thanks

Powershell v2 remoting and delegation

I have installed Powershell V2 on 2 machines and run Enable-PsRemoting on both of them. Both machines are Win 2003 R2 and are joined to the same active directory domain and I can successfully run ...

PowerShell -match operator and multiple groups

I have the following log entry that I am processing in PowerShell I m trying to extract all the activity names and durations using the -match operator but I am only getting one match group back. I m ...

热门标签