Skip to content

Commit bd5168e

Browse files
committed
Create PS1_Method_Overloading.md
1 parent 0d81d8b commit bd5168e

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

AZURE/PS1_Method_Overloading.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
$method = @{ param([int32] $t) if ($t -ge 0) { return $this.substring($t)) } else { return $this.substring(($this.Length + $t)) } }
2+
3+
$mymethod = @{
4+
MemberName = 'splice';
5+
MemberType = 'ScriptMethod';
6+
# Note the use of param() to define the method parameter and
7+
# the use of $this to access the instance at hand.
8+
Value = { param([int32] $t) if ($t -ge 0) { return $this.substring($t)) } else { return $this.substring(($this.Length + $t)) } }
9+
Force = $true
10+
}
11+
12+
#
13+
# The following example defines a new .myLower() method and attaches it to strings
14+
#
15+
$mymethod = @{
16+
MemberName = 'myLower';
17+
MemberType = 'ScriptMethod';
18+
# Note the use of param() to define the method parameter and
19+
# the use of $this to access the instance at hand.
20+
Value = { param([int32] $t) $this.toLower() }
21+
Force = $true
22+
}
23+
24+
Update-TypeData -TypeName 'string' @mymethod
25+
26+
27+
28+
#
29+
# Slice() https://github.com/PowerShell/PowerShell/issues/14533
30+
#
31+
# String slice()
32+
$stringMethod = @{
33+
MemberName = 'slice';
34+
MemberType = 'ScriptMethod';
35+
Value = { param([int32] $t) $( if ($t -ge 0) {$this.substring($t)} else {$this.substring(($this.Length + $t))}) }
36+
Force = $true
37+
}
38+
Update-TypeData -TypeName 'string' @stringMethod
39+
40+
# System.Array slice()
41+
$arrayMethod = @{
42+
MemberName = 'slice';
43+
MemberType = 'ScriptMethod';
44+
Value = { param($t) $this[$t] }
45+
Force = $true
46+
}
47+
Update-TypeData -TypeName 'System.Array' @arrayMethod

0 commit comments

Comments
 (0)