0

I am trying to create object using the foreach loop in PowerShell. Tried using "while" loop, it failed as well. Apparently, the looping methods are not allowing me to create objects...

Without further ado...

I have two scripts - Class.psm1 and Main.ps1.

On Class.psm1

Class Car {
    [string]$brand
    [string]$model
    [string]$color

    #Constructor
    Car ([string]$brand) {
         $this.brand = $brand

         switch -wildcard ($this.brand) {
             ('Toyota') {$this.model = 'ABC'; $this.color = 'red'; break}
             ('Honda') {$this.model = 'FGH'; $this.color = 'blue'; break}
         }
    }
}

And on Main.ps1

Using module ".\Class.psm1"

$AllCars = {'Toyota', 'Honda'}
[array]$Objects = @()

foreach ($car in $AllCars) {
    $temp = New-Object Car("$car")
    $Objects += $temp
}

The output from Main.ps1, is that $Objects are just returning back "Toyota" and "Honda", instead of objects (and the properties it supposed to have).

However, if I were to just create the object individually, it will works fine.

For example:

$temp = New-Object Car('Toyota')
$Objects += $temp
$temp = New-Object Car('Honda')
$Objects += $temp

However, this is too manual work or rather unpractical.

May I know in which area did the codes went wrong...? How do I create the objects within the loop?

4
  • There's a bunch of syntax errors in Class.psm1 - all properties and parameters need $ in front of the name Commented Mar 9, 2019 at 14:24
  • Hi Jessen, sorry, I typed in manually here (as the data was all made up) but in the codes are close to my exact codes with the correct syntax and so on. Thanks for spotting it, will change it here. Commented Mar 9, 2019 at 14:28
  • 1
    Did you perhaps edit your Class and tried without restarting PowerShell? Once loaded, changes to it are not recognized. Commented Mar 9, 2019 at 15:00
  • Hi Theo.. yes, you are right. Somehow, the Class file is the one needed a restart. It works afterwards. Thanks! Commented Mar 11, 2019 at 8:02

1 Answer 1

5

This issue is you are using {'Toyota', 'Honda'} instead of ('Toyota', 'Honda')

{'Toyota', 'Honda'} is a code block. When you pass it to New-Object Car("$car") it is actually passing New-Object Car("'Toyota', 'Honda'")

$AllCars = ('Toyota', 'Honda')
[array]$Objects = @()

foreach ($car in $AllCars) {
    $temp = New-Object Car("$car")
    $Objects += $temp
}

Since i was asked why the kangaroo code I decided to post a shorter response

$Objects = 'Toyota', 'Honda' | %{
    New-Object Car("$car")
}
Sign up to request clarification or add additional context in comments.

7 Comments

Why so kangaroo code? Just use $AllCars = ('Toyota', 'Honda'); <#PleaseTypeANewLineHere#>; $Objects = foreach ($car in $AllCars) { New-Object Car($car) }
I was using his code above and just changed the one line. I didn't write anything different then what he had other then replace {} with (). So if you want to ask about kangaroo code ask the poster.
I'd leave out the brackets too, you just need $AllCars = 'Toyota', 'Honda' to declare the string array.
I beg your pardon for my abusive wording. My poor vocabulary translated it as 'too complex code' without any rude or opprobrious connotation. Sorry.
@JosefZ No Problem Sorry for taking it the wrong way :)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.