question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Class Constructor Error - Can't Set Variable Values

See original GitHub issue

Prerequisites

Steps to reproduce

I’m writing a PowerShell class as follows:

class FileTransferStatus {

    [System.IO.FileInfo[]]$files
    $totalBytesToTransfer
    $bytesLeftToTransfer

    FileTransferStatus([System.IO.FileInfo[]]$files)
    {
        this.$totalBytesToTransfer = 0
        foreach ($file in $files) {
            this.$totalBytesToTransfer += $file.length
        }
        this.$bytesLeftToTransfer = this.$totalBytesToTransfer
    }
}

Expected behavior

No error message

Actual behavior

Visual Studio Code reports an error on the last line of the constructor, which tries to initialize $bytesLeftToTransfer. The message is:

**Variable is not assigned in the method.**

Error details

The error is from Visual Studio Code during editing, not from running the script.

Environment data

Name                           Value
----                           -----
PSVersion                      7.3.4
PSEdition                      Core
GitCommitId                    7.3.4
OS                             Microsoft Windows 10.0.19045
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Visuals

image

Issue Analytics

  • State:closed
  • Created 3 months ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
aksarbencommented, Jun 6, 2023

Wow. I feel like I’m sipping from the proverbial fire hose.

I must have stared at documentation for an hour without seeing this misplaced dollar sign. Glad your eyes are better than mine.

I don’t know who at Microsoft edits documentation, but this is a good example of where clear error messages are invaluable.

Thanks for the help!

0reactions
mklement0commented, Jun 7, 2023

Reflecting more on the potential to improve the error message: I think it can be improved:

Read more comments on GitHub >

github_iconTop Results From Across the Web

PowerShell Class Constructor - Can't Assign Variable Value
I'm getting an error on the last line of the constructor, which tries to initialize $bytesLeftToTransfer. The message is: Variable is not ......
Read more >
Can't assign a value to a variable in constructor
Hello, I wanted to know why I can't assign my variable to value in constructor? MainCharacter.h UCLASS() class GAME_API AMainCharacter ...
Read more >
Despite instantiating a variable in my constructor of ...
I have developer console open, and I look in the log, but it only tells me what I already knew: the error is...
Read more >
constructor - JavaScript - MDN Web Docs - Mozilla
A class method called constructor cannot be a getter, setter, async, or generator. A class cannot have more than one constructor method.
Read more >
Class Constructor Methods - MATLAB & Simulink
A constructor method is a special function that creates an instance of the class. Typically, constructor methods accept input arguments to assign the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found