Quantcast
Channel: SAPIEN Forums
Viewing all articles
Browse latest Browse all 512

PowerShell Studio • Unhandled Exception error when typing a dot after my custom class

$
0
0
Product: PowerShell Studio 2024 (64 Bit)
Build: v5.8.247
OS: Windows 10.0.19045
PS Version(s): 7.4.4 (tried it with 5.1.19041.1 too and get the exact same error)

*** Please add details and screenshots as needed below. ***

So, I created a couple custom classes and, I can instantiate them just fine. However, once instantiated into a variable, as soon as I type a dot after that variable, I get the attached Unhandled Exception error. It seems to be coming from the SnippetManager maybe? I don't know.

Does anybody know what's causing this and how to fix it?

What's weird is that, once I dismiss the error, I can continue to use the class and it's methods just fine, but every time I type a dot after an instance of that class, I get the error.

The classes I've created and the code I'm typing that causes the error are here:

Code:

class DT_SyntheticTestLocation {
[bool]$enabled
[string]$id #required

# Constructor requiring only ID field (which is a required param of this object)
DT_SyntheticTestLocation([string]$id)
{
$this.id = $id;
}

# Constructor that allows the optional property of Enabled
DT_SyntheticTestLocation([string]$id, [bool]$enabled)
{
$this.enabled = $enabled;
$this.id = $id;
}
}
class DT_SyntheticTestLocationsList {
# Static property to hold the list
static [System.Collections.Generic.List[DT_SyntheticTestLocation]]$DT_SyntheticTestLocations

# Static method to initialize the list of DT_SyntheticTestLocations. Called in the other
# static methods to avoid needing to explicit initialize the value.
static [void] Initialize() { [DT_SyntheticTestLocationsList]::Initialize($false) }
static [bool] Initialize([bool]$force)
{
if ([DT_SyntheticTestLocationsList]::DT_SyntheticTestLocations.Count -gt 0 -and -not $force)
{
return $false
}

[DT_SyntheticTestLocationsList]::DT_SyntheticTestLocations = [System.Collections.Generic.List[DT_SyntheticTestLocation]]::new()

return $true
}
# Ensure a DT_SyntheticTestLocation is valid for the list.
static [void] Validate([DT_SyntheticTestLocation]$DT_SyntheticTestLocation)
{
$Prefix = @(
'DT_SyntheticTestLocation validation failed: DT_SyntheticTestLocation must be defined with the id property, but'

)
if ($null -eq $DT_SyntheticTestLocation) { throw "$Prefix was null" }
if ([string]::IsNullOrEmpty($DT_SyntheticTestLocation.id))
{
throw "$Prefix id wasn't defined"
}

}
# Static methods to manage the list of DT_SyntheticTestLocations.
# Add a DT_SyntheticTestLocation if it's not already in the list.
static [void] Add([DT_SyntheticTestLocation]$DT_SyntheticTestLocation)
{
[DT_SyntheticTestLocationsList]::Initialize()
[DT_SyntheticTestLocationsList]::Validate($DT_SyntheticTestLocation)
if ([DT_SyntheticTestLocationsList]::DT_SyntheticTestLocations.Contains($DT_SyntheticTestLocation))
{
throw "DT_SyntheticTestLocation '$DT_SyntheticTestLocation' already in list"
}

$FindPredicate = {
param ([DT_SyntheticTestLocation]$b)

$b.id -eq $DT_SyntheticTestLocation.id

}.GetNewClosure()
if ([DT_SyntheticTestLocationsList]::DT_SyntheticTestLocations.Find($FindPredicate))
{
throw "DT_SyntheticTestLocation '$DT_SyntheticTestLocation' already in list"
}

[DT_SyntheticTestLocationsList]::DT_SyntheticTestLocations.Add($DT_SyntheticTestLocation)
}

# Clear the list of DT_SyntheticTestLocations.
static [void] Clear()
{
[DT_SyntheticTestLocationsList]::Initialize()
[DT_SyntheticTestLocationsList]::DT_SyntheticTestLocations.Clear()
}

# Find a specific DT_SyntheticTestLocation using a filtering scriptblock.
static [DT_SyntheticTestLocation] Find([scriptblock]$Predicate)
{
[DT_SyntheticTestLocationsList]::Initialize()
return [DT_SyntheticTestLocationsList]::DT_SyntheticTestLocations.Find($Predicate)
}

# Find every DT_SyntheticTestLocation matching the filtering scriptblock.
static [DT_SyntheticTestLocation[]] FindAll([scriptblock]$Predicate)
{
[DT_SyntheticTestLocationsList]::Initialize()
return [DT_SyntheticTestLocationsList]::DT_SyntheticTestLocations.FindAll($Predicate)
}

# Remove a specific DT_SyntheticTestLocation.
static [void] Remove([DT_SyntheticTestLocation]$DT_SyntheticTestLocation)
{
[DT_SyntheticTestLocationsList]::Initialize()
[DT_SyntheticTestLocationsList]::DT_SyntheticTestLocations.Remove($DT_SyntheticTestLocation)
}

# Remove a DT_SyntheticTestLocation by property value.
static [void] RemoveBy([string]$Property, [string]$Value)
{
[DT_SyntheticTestLocationsList]::Initialize()
$Index = [DT_SyntheticTestLocationsList]::DT_SyntheticTestLocations.FindIndex({
param ($b)
$b.$Property -eq $Value
}.GetNewClosure())
if ($Index -ge 0)
{
[DT_SyntheticTestLocationsList]::DT_SyntheticTestLocations.RemoveAt($Index)
}
}
}
Then I create an instance of the class like this:
$locList = [DT_SyntheticTestLocationsList]::new();

Then I create a couple items using my other class that I will add to the $locList object like this:

Code:

$locItem1 = [DT_SyntheticTestLocation]::new('Loc1', $true)
$locItem2 = [DT_SyntheticTestLocation]::new('Loc2', $true)
Then I add those to the $locList instance of my class like this:

Code:

$locList::Add($locItem1)
$locList::Add($locItem2)
Then I want to look at the DT_SyntheticTestLocations static property of the class by assigning it to a variable like this:

Code:

$locs = $locList::DT_SyntheticTestLocations
Then, I get the attached error if I call that $locs variable with a dot right after it, like this:

Code:

$locs.
boom, as soon as I type that dot, PS Studio throws the error. I don't get that error in PowerShell ISE or in VS Code. Only in PS Studio do I see it.

Also, I was on a slightly older version of PS Studio before and still got the error. Before submitting this post I wanted to make sure I was on the most current version so I updated and tried again, but yeah. Still got it. So this error isn't unique to this newest version of PS Studio is my point. Same with PowerShell v7. I was on 7.4.2 when I first got this error, so I upgraded to the latest version of v7 (7.4.4) and still got the error.

Oh, and I didn't come up with this code on my own. I literally copy/pasted it from the Microsoft about Classes documentation example on Book, Books, BookList. I just did a find/replace with a few words to make it fit my needs, but I edited that code in no other way and I've only tried using it in the way they demonstrated in their docs (just with my class names and properties of course).
ErrorLog.txt

Statistics: Posted by xtraspecialj — Mon Jul 29, 2024 6:37 am



Viewing all articles
Browse latest Browse all 512

Trending Articles