I've got a bit of a query on condensing some code. Using it in a function or two I am writing. First up some examples.
This is the If/ElseIf method.
If ($aVar -eq 1) {
If ($bVar -le $cVar) {
# code
# code
# code
# code
}
}
ElseIf ($aVar -eq 2) {
If ($bVar -gt $cVar) {
# code
# code
# code
# code
}
}
And here's the Switch method.
Switch ($aVar) {
{$_ -eq 1} {
If ($bVar -le $cVar) {
#code
#code
#code
#code
}
}
{$_ -eq 2} {
If ($bVar -gt $cVar) {
#code
#code
#code
#code
}
}
Is it possible to condense this at all?
The #code sections are the exact same, so was wondering if there'd be a way of only writing it once instead of having it twice in either method used.
Thanks!
How about using the -OR and -AND arguments?
If(($aVar -eq 1 -and $bVar -le $cVar) -or ($aVar -eq 2 -and $bVar -gt $cVar)){
#code
#code
#code
#code
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.