Class Constructor Error - Can't Set Variable Values
See original GitHub issuePrerequisites
- Write a descriptive title.
- Make sure you are able to repro it on the latest released version
- Search the existing issues.
- Refer to the FAQ.
- Refer to Differences between Windows PowerShell 5.1 and PowerShell.
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
Issue Analytics
- State:
- Created 3 months ago
- Comments:7 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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!
Reflecting more on the potential to improve the error message: I think it can be improved: