I'm working on a function that compares two objects so it detects them if they are identical. However I would like it to also work with other types like strings or integers.
C++ lets you declare different functions with the same name to handle function calls with different input types in a different way. I'm aware of the existence of Parameter Sets, however as far as I know the user is the one who must specify which Parameter set he is using.
I'm trying to do something like this
function Compare-Objects{
Param(
[Parameter(Mandatory=$true,
Position=0,
ParameterSetName = "Hashtables")]
[ValidateNotNullOrEmpty()]
[Hashtable]$Item1Hash,
[Parameter(Mandatory=$true,
Position=0,
ParameterSetName = "Integers")]
[ValidateNotNullOrEmpty()]
[int]$Item1int,
[Parameter(Mandatory=$true,
Position=1,
ParameterSetName = "Hashtables")]
[ValidateNotNullOrEmpty()]
[Hashtable]$Item2Hash,
[Parameter(Mandatory=$true,
Position=1,
ParameterSetName = "Integers")]
[ValidateNotNullOrEmpty()]
[Hashtable]$Item2Int
)
if($PSCmdlet.ParameterSetNamePositionv -match "Integers"){ Return ($Item1Int -eq $Item2Int)}
else{
#do some other stuff with $Item1Hash and $Item2Hash
}
}
Extra points if I can also name the variables the same (So $Item1Hash
and $Item1Int
become both $Item1
with the appropriate type assigned)