As I'm ramping up on PowerShell and exploring custom classes, I keep hitting this weird Exception that persists 'til I make any change.
> PS C:\Users\Purpl_000> C:\PS\Node\NodeTest.ps1 Cannot convert the
> "Node" value of type "Node" to type "Node". At
> C:\PS\Node\NodeTest.ps1:5 char:1
> + [Node]$node1 = New-Object Node -Property @{Next=$null; Value=3};
> + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> + CategoryInfo : MetadataError: (:) [], ArgumentTransformationMetadataException
> + FullyQualifiedErrorId : RuntimeException Exception setting "Next": "Cannot convert the "Node" value of type "Node" to type
> "Node"." At C:\PS\Node\NodeTest.ps1:9 char:1
> + $node2.Next = $node1;
> + ~~~~~~~~~~~~~~~~~~~~
> + CategoryInfo : NotSpecified: (:) [], SetValueInvocationException
> + FullyQualifiedErrorId : ExceptionWhenSetting New-Object : The value supplied is not valid, or the property is read-only. Change the
> value, and then try again. At C:\PS\Node\NodeTest.ps1:13 char:16
> + [Node]$node3 = New-Object Node -Property @{Next=$node2; Value=9};
> + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> + CategoryInfo : InvalidData: (:) [New-Object], Exception
> + FullyQualifiedErrorId : SetValueException,Microsoft.PowerShell.Commands.NewObjectCommand
> Cannot find an overload for "new" and the argument count: "2". At
> C:\PS\Node\NodeTest.ps1:22 char:1
> + [Node]$node4 = [Node]::new($node3, 12);
> + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> + CategoryInfo : NotSpecified: (:) [], MethodException
> + FullyQualifiedErrorId : MethodCountCouldNotFindBest
Here's Node.ps1
class Node
{
[Node]$Next;
[int]$Value;
# .ctor
Node([Node]$next, [int]$value)
{
$this.Next = $next;
$this.Value = $value;
}
# default .ctor
Node() {}
static [int] Method1([Node]$n1)
{
return $n1.Value;
}
static [Node] Method2([Node]$n2)
{
$n2.Value = $n2.Value + 5;
return $n2.Value;
}
}
Here's NodeTest.ps1
. 'C:\PS\Node\Node.ps1';
[Node]$node1 = New-Object Node -Property @{Next=$null; Value=3};
[Node]$node2 = [Node]::new();
$node2.Next = $node1;
$node2.Value = 5;
[Node]$node3 = New-Object Node -Property @{Next=$node2; Value=9};
[Node]$node4 = [Node]::new($node3, 12);
I'm working in the PowerShell ISE. Node.ps1 is saved and ran successfully. NodeTest.ps1 is also saved but blows sometimes up until I do any seeming irrelevant change to the file, save, then Run Script again. At this point, it works fine again. Examples of changes that has resolved the issue: (1) Adding an extra line of blank space, (2) Removing an extra line of blank space, (3) Adding a comment, etc..
I understand
New-Object : The value supplied is not valid, or the property is read-only. Change the value, and then try again.
through my testing of what happens when you use the incorrect types but not quite sure why I'm intermittently seeing this. An easy example of intentionally repro'ing that error by passing an int where a Node is expected. :)
[Node]$node3 = New-Object Node -Property @{Next=5; Value=9};
The follow does confuse me. How can a Node not be a Node then magically a Node again after adding a blank line to NodeTest.ps1?
Exception setting "Next": "Cannot convert the "Node" value of type "Node" to type "Node"."
Any information, explanation, or education would be extremely appreciated!
Thanks all! Michael