Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Struct\Class Creation

Reply
 
Old 02-15-2007   #1 (permalink)
PMcG


 
 

Struct\Class Creation

Hi,
I have looked in the docs that come with PowerShell and been searching on
the web to see if it is possible to define my own struct or class within a
script. Have not come across any guidance indicating if this is possible or
not, i'm guessing it is not and would like to confirm this. Can work around
this but is a bit cumbersome when i have a collection of logical entities
that i need to spread across a number of arrays. I guess i expected this as
i'm currently inclined to expect that a lot of the functionality from Ruby
would be available within PowerShell scripting.


Thanks in advance
Pat

My System SpecsSystem Spec
Old 02-16-2007   #2 (permalink)
Keith Hill [MVP]


 
 

Re: Struct\Class Creation

"PMcG" <PMcG@discussions.microsoft.com> wrote in message
news:8983A578-ABDD-4505-85D9-F4A6B257D966@microsoft.com...
> Hi,
> I have looked in the docs that come with PowerShell and been searching on
> the web to see if it is possible to define my own struct or class within a
> script. Have not come across any guidance indicating if this is possible
> or
> not, i'm guessing it is not and would like to confirm this. Can work
> around
> this but is a bit cumbersome when i have a collection of logical entities
> that i need to spread across a number of arrays. I guess i expected this
> as
> i'm currently inclined to expect that a lot of the functionality from Ruby
> would be available within PowerShell scripting.
>


In PowerShell you can't create classes or structs in the typical C#/VB
sense. However you can use hashtables to create name/value pairs aka a
property bag. The nice thing about PowerShell / .NET hashtable is that the
value can refer to another hashtable giving you a way to create object
hierarchies. However if you need to plug this into another .NET type that
is expecting a particular type then you are outta luck unless you want to
use Reflection.Emit or LCG (which isn't really using PowerShell anymore) to
gen up code for this purpose.

--
Keith


My System SpecsSystem Spec
Old 02-25-2007   #3 (permalink)
PMcG


 
 

Re: Struct\Class Creation

Keith,
I came across an example where you can do something like this
$MyObj = new-object System.Object
Add-Member -inputobject $MyObj -membertype noteproperty -name Property1
-value 1

Add-Member -inputobject $MyObj -membertype noteproperty -name Property2
-value 1,2,3

Add-Member -inputobject $MyObj -membertype noteproperty -name Property3
-value "......."

write-output $MyObj
write-output $MyObj.Property1

I havn't spend to much time looking into this but works for me


Pat
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
When a class is both an inherited class of another, and alsoimplements an interface method .NET General
Q: How are ConfigurationProperty default values assigned toclass/struct attributes? .NET General
win32_pingstatus class / dns class PowerShell
PDF Creation? Vista General
How to map openssl obtained private key parameters to RSAParameters struct members? .NET General


Vista Forums 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 Ltd

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