![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
|
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.
br> br> |
| |||||||
![]() |
| | Thread Tools | Display Modes |
| | #1 (permalink) |
| 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; } |
| | #2 (permalink) |
| 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; > } > > |
| | #3 (permalink) |
| 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 } } |
| | #7 (permalink) |
| 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 |
| | #8 (permalink) |
| 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; > } > > |
| |
| |
![]() |
| 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 |