'powershell initiate a class with a variable variable

got a class that I need to initiate an uncertain nr. of times, depending on how many days you need in this week, so from 1 - 7 (hard coded variable with 3 for testing purpose)

But how can I call the class constructor with a dynamic variable? the error says:

The assignment expression is not valid. The input to an assignment operator must be an object that is able to accept assignments, such as a variable or a property.

I assume its, because it sees the $($tmp) as a string and not a variable, tried the get-variable -valueonly command but no luck.

Kind regads Per Andersen

the code:

class Day
{
    [string] $Init

    
    [void] SetInit($Init)
    {
        $this.Init += $Init;
    }
}
$count = 3 #(will be changed to dynamic number when all is rdy)

for ($i=1; $i -le $count; $i++)
{
    $tmp = "`$NewDay$i"
    $($tmp) = [Day]::new();
}

ps removed some code to make it easy to read, but same problem.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source