I have been working on a project in Powershell that leverages the Object Oriented nature of the language.
Here's what I am trying to do:
I have created several classes with distinct properties. We can call one of these classes 'ClassA'.
One of the properties of these classes is another class. We can call this 'ClassB'.
I have created several instances of each of these classes.
I need to set parameters of the class, which is a parameter of another class. So, for example: ClassAInstance.ClassBInstance.Property1
I am using a class method to update these parameters.
What i am finding is that when I set one of these lowest level properties, it is updating that parameter on all the instances of the class that previously exist.
What's weird is that the properties of ClassA instances (that are not type ClassB) get updated fine without affecting any existing ClassA instances. The issue is only with properties of ClassB instances, which are properties of ClassA instances.
Any idea why this might be happening?
I apologize for the lack of specifics. It's tough because the code has become pretty complicated, and there is also quite a bit of proprietary information contained within.
Class XBlock
{
[string]$Name
}
Class YBlock: XBlock
{
[string]$Name = 'Y1'
[float]$High_Scale = 100
}
Class XClass
{
[string]$Name = ''
[System.Collections.ArrayList]$CodeBlock
SetXParams()
{
$blocknames = New-Object System.Collections.Arraylist
for ($i = 1; $i -le $this.XBlocks; $i++)
{
$blocknames.add("X$i")
}
$blocknames | %{
$this.$_ = New-Object XBlock
$pattern = -join ('Attribute_Instance Name="', $_, '/High_Scale')
$match = $this.CodeBlock | Select-String -Pattern $pattern -Context 0, 2 -ErrorAction SilentlyContinue
If ($match.count -gt 0)
{
$this.$_.High_Scale = ($match[0].Context.PostContext[1]).split('=').split(' ')[7]
}
}
}
}
class YClass: XClass
{
[int]$YBlocks = 1
[YBlock]$Y1
}
Class XModule
{
[string]$Name = ''
[string]$Class = ''
[System.Collections.ArrayList]$CodeBlock
[int]$XBlocks = 0
[System.Collections.ArrayList]$CodeBlock
SetModuleParams([XModule]$XModule)
{
$this.Name = $XModule.Name
$this.CodeBlock = $XModule.CodeBlock
$this.ClassObject = ($Global:AllClasses | ? { $_.Name -eq $this.Class }).PSObject.Copy()
}
SetYParams()
{
$blocknames = New-Object System.Collections.Arraylist
for ($i = 1; $i -le $this.XBlocks; $i++)
{
$blocknames.add("Y$i")
}
$blocknames | %{
$this.$_ = New-Object YBlock
$this.$_ = $this.ClassObject.$_.PSObject.Copy()
$pattern = -join ('(Attribute Instance Name="', $_, '.*High_Scale)')
$match = $this.CodeBlock | Select-String -Pattern '(Attribute Instance Name=".*High_Scale)' -Context 0, 2 #-ErrorAction SilentlyContinue -SimpleMatch $true
If ($match.count -gt 0)
{
$this.$_.High_Scale = ($match[0].Context.PostContext[1]).split('=').split(' ')[7]
}
}
}
}