-Programming: Learning about "classes" ...

Hello Folks,

I'm expanding my knowledge in learning classes in powershell... I have written this basic  PS example

class Books {
    [string]$title 
    [string]$author
    [int]$pages   
}

$books1 = [books]::new()
$books1.title = "Harry Potter"
$books1.author = "JK Rowling"
$books1.pages = 309

$books2 = [books]::new()
$books2.title = "Scott Pilgrim vs. the World"
$books2.author = "Bryan Lee O'Malley"
$books2.pages = 168

$books2 | ? {$_.title -like "*scott*"}

Open in new window


this will display the book from book 2 titled like "scott"...
Does anybody have any idea on how can I tell the program to search within all the books and find any "X" Title... I was thinking "foreach" but i'm not sure I could squeeze it in...? or if you have a c# approach that would be great also :)

Thanks in advance!
LVL 3
ivan rosaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
That's got nothing to do with classes, only with the way you store the books.
Use an array, not individual variables.
Simplest solution in PowerShell: you define an array as @(), then you can just use the + operator to add elements. It's not efficient, but works just fine for the usual tasks wher you don't have to handle millions of elements.
And while you're learning: don't use aliases (%, ?, dir, where, gc, ...) in scripts; always write out the full cmdlet name. Use aliases only in the command line, where you can be sure that nobody redefined one or more of them.
Class Books {
    [string]$title 
    [string]$author
    [int]$pages   
}

$books = @()

$book = [books]::new()
$book.title = "Harry Potter"
$book.author = "JK Rowling"
$book.pages = 309
$books += $book

$book = [books]::new()
$book.title = "Scott Pilgrim vs. the World"
$book.author = "Bryan Lee O'Malley"
$book.pages = 168
$books += $book

$books | Where-Object {$_.Title -like "*scott*"}

Open in new window


Or you can use an ArrayList and its Add method:
Class Books {
    [string]$title 
    [string]$author
    [int]$pages   
}

$books = New-Object -TypeName System.Collections.ArrayList

$book = [books]::new()
$book.title = "Harry Potter"
$book.author = "JK Rowling"
$book.pages = 309
[void]$books.Add($book)

$book = [books]::new()
$book.title = "Scott Pilgrim vs. the World"
$book.author = "Bryan Lee O'Malley"
$book.pages = 168
[void]$books.Add($book)

$books | Where-Object {$_.Title -like "*scott*"}

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Alfredo Luis Torres SerranoASP .Net DeveloperCommented:
I would recommend you to read and study  a concrete example that could give you a full scope on how to work with classes on powershell

please follow this link
ivan rosaAuthor Commented:
Thanks for your swift response @oBdA. as usual you are always on point!

Thanks for the tips also as well as @Alfred I appreciate it
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.