header
header Register : : Login header
header
divider
menuleft
menuright
submenu
left

[August 25th, 2008] Check the home page regarding PowerShell related news from a brand new sponsor: Idera

Displaying constant values for properties of objects
Last Post 21 Nov 2007 11:11 PM by dalelane. 6 Replies.
Printer Friendly
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
dalelaneUser is Offline
New Member
New Member
Posts:4

--
10 Nov 2007 05:20 PM  
I'm writing cmdlets which Get custom .NET objects.

Some of the properties of these objects are 'int' values - one of a number of constants. When displaying the value in a table, it would be better if a friendly description of the constant is returned instead of the int value.

e.g.

C:\> Get-Animal
Name           AnimalType
-------------------------------
Dolly          Sheep
Nelly          Elephant
Mr Ed          Horse

instead of


C:\> Get-Animal
Name           AnimalType
-------------------------------
Dolly          1
Nelly          2
Mr Ed          3

I can think of different ways of doing this, but they all revolve around having another property to store the string representation.

E.g.

An additional string property in the C# class for my object

   public class Animal 
   {
      public int AnimalType;
   }

   public class Sheep : Animal 
   {
      public string AnimalTypeStr = "Sheep";
   }
An alias property in the typedata
adding a script property like this, and using Update-TypeData to use it:

   <ScriptProperty>
      <Name>AnimalTypeStr</Name>
      <GetScriptBlock>
          if ($this.AnimalType -eq [MyNamespace]::AT_SHEEP) { return "Sheep" }
          if ($this.AnimalType -eq [MyNamespace]::AT_ELEPHANT) { return "Elephant" }
          if ($this.AnimalType -eq [MyNamespace]::AT_HORSE) { return "Horse" }
       </GetScriptBlock>
    </ScriptProperty>

This seems wrong - as I don't really want to be storing more data in my classes - I just want to control their formatting.

Is there a best-practice for this?

Can I control the formatting so that the 'int's can be displayed in different ways?
DonJUser is Offline
PowerShell MVP
Basic Member
Basic Member
Posts:134

--
17 Nov 2007 06:19 PM  
Well, I don't know about "best practice," but you're on the right track. A custom type file is pretty much how PowerShell itself does this.

The built-in example you want is get-Service, which returns a ServiceController object. The status property for this is displayed as "Stopped" or "Running" although the underlying property is actually a number. Now, Get-Service doesn't actually accomplish this magic with a .ps1xml file - maybe it's something built-in? - but you could definitely take the .ps1xml approach as you've done. A Switch construct might be more appropriate.

I'll ask someone on the PowerShell team, though, and my fellow MVPs, to see if there's a better answer - I'm curious, myself.
DonJUser is Offline
PowerShell MVP
Basic Member
Basic Member
Posts:134

--
17 Nov 2007 06:26 PM  
Actually, I may have typed too fast. What Get-Service has in its Status property is a System.ServiceProcess.ServiceControllerStatus, which is an enumeration from the class itself. Have you declared your constants as an actual enumeration class like this? I'm theorizing that if your output object's property is of the enumeration type, then the member name of the enumeration will be displayed rather than the corresponding numeric value.
dalelaneUser is Offline
New Member
New Member
Posts:4

--
18 Nov 2007 01:14 PM  
I'm theorizing that if your output object's property is of the enumeration type, then the member name of the enumeration will be displayed rather than the corresponding numeric value.
That sounds like a good plan.

I don't actually have access to change the constants that I'm using (they are from an external product's API), but I'll write a quick intermediate layer that maps the existing constants to enums and give that a try.

I'll add a new post to say how it worked when I've tried it.

Many thanks

dalelaneUser is Offline
New Member
New Member
Posts:4

--
20 Nov 2007 10:49 PM  
that did the trick

thanks again for your help

kind regards

Dale
DonJUser is Offline
PowerShell MVP
Basic Member
Basic Member
Posts:134

--
21 Nov 2007 05:38 PM  
Oh, good. Nice when theory pans out :). So that should be a best practice for cmdlet development: Expose enumerations AS enumerations. Feel free to add it to the Best practices wiki here - if you don't want to, I'll try and get to it!
dalelaneUser is Offline
New Member
New Member
Posts:4

--
21 Nov 2007 11:11 PM  
I've added something to the wiki here

Thanks again for your help
You are not authorized to post a reply.

Active Forums 4.1
right
   
footer Sponsored by Quest Software • SAPIEN Technologies • ShellTools, LLC • Microsoft Windows Server 2008 footer
footer