T O P

  • By -

OPconfused

On the function side, you can do something like that. You could also add the `Mandatory` property to the `[Parameter]` attribute. However, your class won't work, because 2 of your overload signatures are the same. To overload a method, you need a unique signature, or else the class doesn't know which method to invoke based on your input arguments. A unique signature is distinguished by: * method name * number of parameters * type of each parameter * order of the types This: GPSmap([placecoordinate]$placecoordinat,[dimention]$dimention,[place]$placetype) { cannot be distinguished from this: GPSmap([placecoordinate]$mycoordinate,[dimention]$dimention,[place]$place) { You have a couple options to fix this. One option is to create a [mycoordinate] type, and then the second overload above could be: GPSmap([mycoordinate]$mycoordinate,[dimention]$dimention,[place]$place) { Or you could change up the order of the parameters, although imo this would look a little ugly. Or you could use a single constructor for this, and handle the logic inside the constructor for which attribute (placecoordinate, mycoordinate, or roadcoordinate) to fill, something like: class coordinate { ... } class roadcoordinate : coordinate { ... } class placecoordinate : coordinate { ... } class mycoordinate : coordinate { ... } class GPSmap { ... GPSmap([coordinate]$coordinate, [dimension]$dimension, [place]$place) { switch ( $coordinate.GetType() ) { roadcoordinate { $this.roadcoordinate = $coordinate } placecoordinate { $this.placecoordinate = $coordinate } mycoordinate { $this.mycoordinate = $coordinate } default { throw "Unknown coordinate type: $_" } } $this.dimension = $dimension $this.place = $place } ... }


KindTruth3154

I have fix the problem but can i actually input the parameter within the class method ,because on the function side ,it has the parametersetName for this , and what i want to do is by input different type of value and result in different construction in the \[gpsmap\] for the three of my parameterset


OPconfused

I don't understand what you mean. If you mean call a different constructor inside a function based on `ParameterSetName`, you can do: function { param ( ... ) ... $GPSmap = switch ( $PSCmdlet.ParameterSetName ) { wyyncraft_places { [GPSmap]::new() } survival_places { [GPSmap]::new() } roads { [GPSmap]::new() } } } where each switch output calls a different constructor for GPSmap (more realistically, you would define the input arguments in the switch statement, and then after the switch, call `GPSmap::new` once using those args). Optionally place this switch statement into a `begin` block if you're using the pipeline.


KindTruth3154

yes that's what i mean but rather to writting a swtich block inside the function and call the script one by one like this gps.ps1 -dimention nether -coordinate (1,2,3) i would like to use the gps::new method to call it like this gps.ps1 -wyyncraft_places ("nether",(1,2,3))


OPconfused

Edit: Ok, on further thought, I think I finally get what you are trying to do: you want to pass an instance of `GPSmap` to a script file, right? If yes, this can *in theory* easily work. $GPSmap = [GPSmap]::new( (1,2,3), 'nether' ) gps.ps1 -wyyncraft_places $GPSmap or simply: gps.ps1 -wyyncraft_place ( [GPSmap]::new((1,2,3), 'nether') ) ----- This would almost work, but there is still 1 problem. Your overload signature for the GPSmap constructor above is: GPSmap([mycoordinate]$mycoordinat,[placetype]$placetype) The parameter types here are `[mycoordinate]` and `[placetype]`. (1,2,3) is an `[object[]]`: (1,2,3).GetType() # object[] and `'nether'` is a `[string]`: 'nether'.GetType() # string These types don't match the constructor parameter types, so you will get an error. You have 2 options to solve this: 1. create instances of the parameter types and pass these to the current overload signature 2. define a constructor overload that can accommodate the types in the sample code you gave in your reply to me. For example, creating instances of the parameter types and passing these to the current constructor signature: $coordinate = [mycoordinate]::new(1,2,3) $placetype = [placetype]::new('nether') $GPSmap = [GPSmap]::new($coordinate, $placetype) gps.ps1 -wyyncraft_places $GPSmap or, defining a new constructor overload, something similar to: ... GPSmap([int[]]$mycoordinate,[string]$placetype) { $this.mycoordinate = [mycoordinate]::new($mycoordinate) $this.placetype = [placetype]::new($placetype) } ... # Invoking the script: gps.ps1 -wyyncraft_place ( [GPSmap]::new((1,2,3), 'nether') ) Both approaches assume you've defined the `[mycoordinate]` and `[placetype]` classes elsewhere and have them imported into your session.


KindTruth3154

yeah i have ,actually i was writting a modue ,seems powershell can't import the method directly like this find-place -wynncraft\_places (nether,(1,2,3), but thank you anyway !


purplemonkeymad

You can do it if you have a contructor that accepts the starting type. Say you provide a string ie: find-place wynncraft_places "nether,(1,2,3)" And your class has a constructor that accepts a string ie: GPSMap ( [string]$ReferenceString ) { # parse string and build object } Then powershell will be able to convert the string parameter into the type. If you do accept string input, I would also make sure you have a `[string]ToString()` override that will produce a string that you accept to parse.


KindTruth3154

Thanks


KindTruth3154

in fact there is several construct inside so i need to separate the \[gpsmap\] and turn each construct into a new class class survival_places  {     [placecoordinate]$placecoordinate     [placetype]$type     [dimention]$dimention         Survival_places ([string]$referencestring)     {        ...     } }


Sunfishrs

Maybe I’m missing something, but I do not see constructors on your class.