Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

What is PS equivalent of switching on an enum value?

Closed Thread
 
Thread Tools Display Modes
Old 10-06-2006   #1 (permalink)
=?Utf-8?B?QW5kcmV3IFdlYmI=?=
Guest


 

What is PS equivalent of switching on an enum value?

Given the C# (pseudo) code below, what would be the best way in PowerShell
script to write the switch statement?

My solution thus far has been to convert the variable to a string, and
switch on the string representation of the enum's items (hoping they haven't
been obfuscated); e.g.

switch ($status.ToString())
{
File { do this }
Folder { do that }
}

TIA

Andrew

----------------------------------------

// The C# original

public enum ItemStatus
{
File,
Folder,
Wildcard,
Unknown,
}

....

ItemStatus status = <one of the enum values>

// How to convert this C# switch statement to PS script?
switch (status)
{
case ItemStatus.File
// do this
break;

case ItemStatus.Folder
// do that
break;
}


Old 10-06-2006   #2 (permalink)
Bruce Payette [MSFT]
Guest


 

Re: What is PS equivalent of switching on an enum value?

PowerShell will automatically do the converstion from string to enum and
vice versa so you don't need to do the ToString(). Here's an example that
shows the various possibilities.

$colors = [consolecolor]::red,[consolecolor]::blue,"blue","green"
switch ($colors)
{
red {"It's red"; continue } # string
match, enums are automatically converted to strings
([consolecolor]::blue) { "It's blue" ; continue} # matchs the enum
value, strings are automatically converted to enums
#
(note the parens around the expression).
green {"It's green"; continue } # strings again
}

Note that this example is using the switch statement's ability to process a
collection. The collection is composed of strings that are the names of
colours and the enum values.

-bruce

--
Bruce Payette [MSFT]
Windows PowerShell Technical Lead
Microsoft Corporation
This posting is provided "AS IS" with no warranties, and confers no rights.
Visit the Windows PowerShell Team blog at:
http://blogs.msdn.com/PowerShell
Visit the Windows PowerShell ScriptCenter at:
http://www.microsoft.com/technet/scr.../hubs/msh.mspx

"Andrew Webb" <AndrewWebb@discussions.microsoft.com> wrote in message
news:A63E687F-340C-466A-A7FB-06DCB6AE276A@microsoft.com...
> Given the C# (pseudo) code below, what would be the best way in PowerShell
> script to write the switch statement?
>
> My solution thus far has been to convert the variable to a string, and
> switch on the string representation of the enum's items (hoping they
> haven't
> been obfuscated); e.g.
>
> switch ($status.ToString())
> {
> File { do this }
> Folder { do that }
> }
>
> TIA
>
> Andrew
>
> ----------------------------------------
>
> // The C# original
>
> public enum ItemStatus
> {
> File,
> Folder,
> Wildcard,
> Unknown,
> }
>
> ...
>
> ItemStatus status = <one of the enum values>
>
> // How to convert this C# switch statement to PS script?
> switch (status)
> {
> case ItemStatus.File
> // do this
> break;
>
> case ItemStatus.Folder
> // do that
> break;
> }
>
>



Old 10-06-2006   #3 (permalink)
Adam Milazzo
Guest


 

Re: What is PS equivalent of switching on an enum value?

Andrew Webb wrote:
> Given the C# (pseudo) code below, what would be the best way in PowerShell
> script to write the switch statement?
>
> My solution thus far has been to convert the variable to a string, and
> switch on the string representation of the enum's items (hoping they haven't
> been obfuscated); e.g.
>
> // How to convert this C# switch statement to PS script?
> switch (status)
> {
> case ItemStatus.File
> // do this
> break;
>
> case ItemStatus.Folder
> // do that
> break;
> }


Well first of all, you don't need to use .ToString(). PowerShell can do
that automatically.

switch($status) # this works
{
File { do this }
Folder { do that }
}

And if you don't want to compare by string, there is this, but I'm not
sure it's any better than the normal PowerShell way of doing it.

# this one doesn't use strings as the clauses, but who knows how
# powershell treats it internally?
switch($status)
{
$([ItemStatus]::File) { do this }
$([ItemStatus]::Folder) { do that }
}
Old 10-06-2006   #4 (permalink)
=?Utf-8?B?QW5kcmV3IFdlYmI=?=
Guest


 

Re: What is PS equivalent of switching on an enum value?

Thanks Bruce. So what would be the best way for the PowerShell script to
"know" about my enum, which comes from one of my own .NET assemblies?



Old 10-06-2006   #5 (permalink)
=?Utf-8?B?QW5kcmV3IFdlYmI=?=
Guest


 

Re: What is PS equivalent of switching on an enum value?

Thanks Bruce. So what would be the best way for the script to 'know' about
my enum, which comes from one of my own C# assemblies?
Old 10-06-2006   #6 (permalink)
=?Utf-8?B?QW5kcmV3IFdlYmI=?=
Guest


 

Re: What is PS equivalent of switching on an enum value?

OK - it just works! I take out the ToString call, and it just works. I
thought I'd have to get a lot fancier to get it to work. Still reaching back
for the old C# compiled environment...

Thanks Bruce and Adam.
Old 10-06-2006   #7 (permalink)
Adam Milazzo
Guest


 

Re: What is PS equivalent of switching on an enum value?

Andrew Webb wrote:
> OK - it just works! I take out the ToString call, and it just works. I
> thought I'd have to get a lot fancier to get it to work. Still reaching back
> for the old C# compiled environment...


You might want to add the "continue" like Bruce did, too.

switch($status)
{
File { do this; continue }
Folder { do that; continue }
}

Otherwise it will "fall through" the switch block and keep trying to
match the other clauses. Sometimes this is what you want; sometimes it's
not.

switch -wildcard ("foo")
{
foo { "a" } # catch the specific case
bar { "b" }
f* { "c" } # catch the general case
}

outputs:
a
c
Old 10-06-2006   #8 (permalink)
James Truher
Guest


 

Re: What is PS equivalent of switching on an enum value?

to say a little more on this, you can also do stuff like this:

PS> $myColor = "RED"
PS> if ( [enum]::GetValues([consolecolor]) -contains $myColor) { "$myColor
is a Color!!!!" } else { "$myColor is not a valid color!" }
RED is a Color!!!!
PS> $myColor = "REDD"
PS> if ( [enum]::GetValues([consolecolor]) -contains $myColor) { "$myColor
is a Color!!!!" } else { "$myColor is not a valid color!" }
REDD is not a valid color!


--
--
James Truher [MSFT]
Windows PowerShell Development
Microsoft Corporation
This posting is provided "AS IS" with no warranties, and confers no rights.

"Andrew Webb" <AndrewWebb@discussions.microsoft.com> wrote in message
news:A63E687F-340C-466A-A7FB-06DCB6AE276A@microsoft.com...
> Given the C# (pseudo) code below, what would be the best way in PowerShell
> script to write the switch statement?
>
> My solution thus far has been to convert the variable to a string, and
> switch on the string representation of the enum's items (hoping they
> haven't
> been obfuscated); e.g.
>
> switch ($status.ToString())
> {
> File { do this }
> Folder { do that }
> }
>
> TIA
>
> Andrew
>
> ----------------------------------------
>
> // The C# original
>
> public enum ItemStatus
> {
> File,
> Folder,
> Wildcard,
> Unknown,
> }
>
> ...
>
> ItemStatus status = <one of the enum values>
>
> // How to convert this C# switch statement to PS script?
> switch (status)
> {
> case ItemStatus.File
> // do this
> break;
>
> case ItemStatus.Folder
> // do that
> break;
> }
>
>



Closed Thread

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
VB6 Equivalent Phil Hunt .NET General 5 05-22-2008 04:22 PM
Enum permissions - access denied Paul19 General Discussion 1 05-12-2008 10:26 AM
'runwait' equivalent Cookiecutter PowerShell 4 04-09-2008 09:34 PM
Equivalent to $0? Charlie Russel - MVP PowerShell 10 06-12-2007 12:51 AM
what is the equivalent of xp pro Lamb Chop Vista General 13 02-09-2007 09:15 PM








Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50