Reputation: 916
I wrote a function to convert the input month parameter to a certain format. such as if I passed 04 to the function and the function will return the "_APR_". the function I wrote is like below:
function GetEnMonth()
{
param([string] $month)
switch ($month)
{
($_ -eq "01"){$result = "_JAN_"}
($_ -eq "02"){$result = "_FEB_"}
($_ -eq "03"){$result = "_MAR_"}
($_ -eq "04"){$result = "_APR_"}
($_ -eq "05"){$result = "_MAY_"}
($_ -eq "06"){$result = "_JUN_"}
($_ -eq "07"){$result = "_JUL_"}
($_ -eq "08"){$result = "_AUG_"}
($_ -eq "09"){$result = "_SEP_"}
($_ -eq "10"){$result = "_OCT_"}
($_ -eq "11"){$result = "_NOV_"}
($_ -eq "12"){$result = "_DEC_"}
default {$result ="_No_Result_"}
}
return [string]$result;
}
Then I use below command to execute the function to get the result:
$mYear = $today.substring(0,4)
$mMonth =$today.substring(4,2)
$mDate = $today.substring(6,2)
$monthInEn = GetEnMonth $mMonth
well, the result is always "_No_Result_", why? below is the exception:
**** Exception type : System.Management.Automation.RuntimeException
**** Exception message : You cannot call a method on a null-valued expression.
Could anyone give me an answer for this? I have searched Google a lot but don't find useful solutions.
Upvotes: 1
Views: 2688
Reputation: 126712
Here's my take on this. I changed the parameter type to Int, with this you'll be able to get process such as "03" or 3. I also added break statements to make the switch work faster.
function GetEnMonth
{
param([int] $month)
switch ($month)
{
1 {"_JAN_"; break}
2 {"_FEB_"; break}
3 {"_MAR_"; break}
4 {"_APR_"; break}
5 {"_MAY_"; break}
6 {"_JUN_"; break}
7 {"_JUL_"; break}
8 {"_AUG_"; break}
9 {"_SEP_"; break}
10 {"_OCT_"; break}
11 {"_NOV_"; break}
12 {"_DEC_"; break}
default {"_No_Result_"}
}
}
Upvotes: 3
Reputation: 72610
This is not an answer, but another way to do the same in one line and with a culture param:
$month = 3
$smonth=[string]::format([System.Globalization.CultureInfo]"en-US", "_{0:MMM}_",[datetime]("$month/01"))
$smonth
_MAR_
Upvotes: 2
Reputation: 60910
Your switch statment is wrong try this:
function GetEnMonth()
{
param([string] $month)
switch ($month)
{
"01" {$result = "_JAN_"}
"02" {$result = "_FEB_"}
"03" {$result = "_MAR_"}
"04" {$result = "_APR_"}
"05" {$result = "_MAY_"}
"06" {$result = "_JUN_"}
"07" {$result = "_JUL_"}
"08" {$result = "_AUG_"}
"09" {$result = "_SEP_"}
"10" {$result = "_OCT_"}
"11" {$result = "_NOV_"}
"12" {$result = "_DEC_"}
default {$result ="_No_Result_"}
}
return [string]$result;
}
Upvotes: 2
Reputation: 16616
This should work:
function GetEnMonth
{
param([string] $month)
switch ($month)
{
"01"{$result = "_JAN_"}
"02"{$result = "_FEB_"}
"03"{$result = "_MAR_"}
"04"{$result = "_APR_"}
"05"{$result = "_MAY_"}
"06"{$result = "_JUN_"}
"07"{$result = "_JUL_"}
"08"{$result = "_AUG_"}
"09"{$result = "_SEP_"}
"10"{$result = "_OCT_"}
"11"{$result = "_NOV_"}
"12"{$result = "_DEC_"}
default {$result ="_No_Result_"}
}
return [string]$result;
}
Upvotes: 3