-Programming: Learning about "classes" ...

ivan rosa
ivan rosa used Ask the Experts™
on
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!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
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

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

Author

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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial