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

Struct\Class Creation

Closed Thread
 
Thread Tools Display Modes
Old 02-15-2007   #1 (permalink)
PMcG
Guest


 

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
Old 02-16-2007   #2 (permalink)
Keith Hill [MVP]
Guest


 

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


Old 02-25-2007   #3 (permalink)
PMcG
Guest


 

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
Closed Thread

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Font Creation Bill Vista General 1 07-23-2008 04:46 PM
PDF Creation? niagara94 Vista General 5 05-28-2008 10:54 AM
How to map openssl obtained private key parameters to RSAParameters struct members? SammyBar .NET General 1 05-23-2008 07:51 AM
XML Document creation ebgreen PowerShell 3 11-05-2007 06:19 PM
help with folder creation stiggy Vista General 1 09-16-2007 08:58 AM








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