Greet
I was writting a code handling coornate with powershell class and i define two method to add different type of place
one with name x y z and other with type x y z
static [psobject]add(
[System.Collections.Generic.List[place]] $places,
[place]$newplace
)
{
[place]::validate($newplace)
return $places + $newplace
}
static[psobject] add (
[System.Collections.Generic.List[place]] $places,
[int]$x,
[int]$y,
[int]$z,
[string]$type
)
{
$newplace = [place]::new(@{
x=$x
y=$y
z=$z
type = $type
})
return $places + $newplace
}
And i call them like this
fisrt method :
$newplaces = [place]::add($places, $newplace)
second method :
$places = [place]::new(@{
places= $places
x=1
y=1
z=1
type="potion merchant"})
first method working well with its overload
but when i try the second method it gives an error of
MethodException: Cannot find an overload for "add" and the argument count: "1".
Entire code :
class place {
[int]$x
[int]$y
[int]$z
[string]$name
[string]$type
static [System.Collections.Generic.List[place]] $places
place() { $this.Init(@{}) }
place([hashtable]$Properties) { $this.Init($Properties) }
# Shared initializer method
[void] Init([hashtable]$Properties) {
foreach ($Property in $Properties.Keys) {
$this.$Property = $Properties.$Property
}
}
static [psobject] calc (
[place]$place1,
[place]$place2
)
{
$distance = ($place1.x-$place2.x )*($place1.x-$place2.x)+($place1.y-$place2.y)*($place1.y-$place2.y)+($place1.z-$place2.z)*($place1.z-$place2.z)
return [distance]::new($place2.name,$distance)
}
static[psobject] find(
[place]$place1,
[place]$place2
)
{
$distance = ($place1.x-$place2.x )*($place1.x-$place2.x)+($place1.y-$place2.y)*($place1.y-$place2.y)+($place1.z-$place2.z)*($place1.z-$place2.z)
return [distance]::new($place2.name,$distance,$place2.x,$place2.y,$place2.z)
}
static[psobject] add (
[System.Collections.Generic.List[place]] $places,
[int]$x,
[int]$y,
[int]$z,
[string]$type
)
{
$newplace = [place]::new(@{
x=$x
y=$y
z=$z
type = $type
})
return $places + $newplace
}
static [void] Validate([place]$newplace){
switch ($newplace.type ) {
"Potion Merchant" { }
"BlackSmith"{ }
"Scroll Merchant"{ }
"Farm"{ }
"Fishing"{ }
"tool Merchant"{ }
"cave"{ }
"mine"{}
Default {
throw $newplace.name + "is not a valid type please try
-Potion Merchant
-BlackSmith
-Scroll Merchant
-Farm
-Fishing
-tool Merchant
-cave
-mine
"
}
}
}
static [psobject]add(
[System.Collections.Generic.List[place]] $places,
[place]$newplace
)
{
[place]::validate($newplace)
return $places + $newplace
}
}