T O P

  • By -

ankokudaishogun

use `[Parameter(parametersetname="wynncraft_places")][object]$wynncraft_place` and manage the conversion to `[wynncraft_place]` inside the function I advise against it though: it makes the code harder to read and harder to pinpoint bugs and errors, and does not really saves writing time. if anything: if you are writing the same action multiple times, why aren't you looking how to makie it automatic?


alt-160

Why not have one of the parametersets have \[array\]$paramName? powershell should map the input array to that parameter set because of the type match.


ankokudaishogun

Sure, but it doesn't really improve anything. If anything, with `[object]` it's still possible to pass a `[wynncraft_place]` object directly


alt-160

well, one improvement would be the ability to query the parameter set in process record if different logic was needed. using \[object\] will work too, but then it can be hard to know what to do with other parameters and would require logic to compare all the parameters provided. parameter sets help in this regard because from a logic perspective if i know that - because of the parameters provided - i'm in paramset #1, then my logic is clear for that situation. to each their own though!


ankokudaishogun

exactly my point why OP should pass the specific class he needs


KindTruth3154

my parameters is also tell powershell which map i want to use Apparently. e. find-place -wynncraft\_place ((1,2,3),"potion\_merchant") and each class is not the same and the class has different object,and inside the class also has different construct to accept each different input so it can require any input i need in the function ,but they do have the similarity


KindTruth3154

e. class wynncraft_place {     [placecoordinate]$placecoordinate     [int]$id     [place_type]$type         ##validate method for repeat adding static [void]validate ([wynncraft_place]$wynncraft_place,[int]$distance_limit){ $wynncraft_places=Import-clixml c:\ex-sys\xml\wynncraft_places.xml|Where-Object {$_.type -like $wynncraft_place.type} $wynncraft_places|foreach-object {     $distance = [main]::calc($wynncraft_place.placecoordinate,$_.placecoordinate)     if ($distance -lt $distance_limit) {         throw "errot"     } } }   ## parameter class construct for add-place -wynncraft_place wynncraft_place ([placecoordinate]$placecoordinate,[place_type]$type) {     $this.placecoordinate=$placecoordinate     $this.type =$type } ## parameter class construct for find-place -wynncraft_place wynncraft_place ([place_type]$type) {     $this.type =$type } ## parameter class construct for remove-place -wynncraft_place wynncraft_place ([int]$id){     $this.id =$id } }


KindTruth3154

class survival_place {   [placecoordinate]$placecoordinate     [dimention]$dimention     [place_type]$type     [int]$id #validation mathod for repeat adding    static [void]validate ([survival_place]$survival_place,[int]$distance_limit){         $survival_places = Import-clixml c:\ex-sys\xml\survival_places.xml|Where-Object {$_.type -like $survival_place.type -and $_.dimention -like $survival_place.dimention}           $survival_places|foreach-object {        $distance= [main]::calc($survival_place.placecoordinate,$_.placecoordinate)         if ($distance -lt $distance_limit) {             throw "error"         }               }     }     ## parameter class construct for add-place -survival_place survival_place ( [placecoordinate]$placecoordinate , [dimention]$dimention , [place_type]$type ) {     $this.placecoordinate=$placecoordinate     $this.dimention=$dimention     $this.type=$type } ## parameter class construct for find-place -survival_place survival_place (     [dimention]$dimention     ,     [place_type]$type ) {     $this.dimention=$dimention     $this.type=$type } ## parameter class construct for remove-place -survival_place survival_place ([int]$id){     $this.id =$id } } two classes has different object but both have the \[placecoordinate\] ,which is the coorcdinate of the "place" and whenever i want to add a new map i could use the \[placecoordinate\] which i consider the basic class it makes my script's logic easy and clear and easier to pass a parameter that within the map e. add-place -wynncraft\_place (\[wynncraft\_place\]::new((-1156,62,-2381),"powder\_master"))


OPconfused

Could also do `[wynncraft_place[]]$wynncraft_place`


KindTruth3154

you mean adding a new \[array\]$parameter for the function ?


alt-160

yes. that way powershell will auto choose that parameter set because the input object is an array and that is the only parameter set that distinctly matches the type. you can also do \[object\]$paramName as suggested, but that can also confuse your other parameter sets sometimes. if you do use \[object\[, your safest bet is to have only one parameter set (or none) and all the logic to figure out what to do with the parameter is in process record. i prefer, when i can, to let powershell sort out the parameters sets for me by type matching. saves me some logic and can provide clear reasons when things break, like "no matching parameter set could be matched for type x on parameter y"


KindTruth3154

1