0

i want to add items to arraylist object inside the class. how to do it. below is powershell code. it doesnt allow. how to achive this with 1. array declared at global level 2. array declared at class level

$logArrayGlobal = [System.Collections.ArrayList]::new()

class LogManager {
    $logArrayClass = [System.Collections.ArrayList]::new()

    LogManager()
    {
        $logArrayGlobal.Add("sada")
        $this.logArrayClass
    }
}
2
  • 1
    Change $logArrayGlobal.Add("sada") to $global:logArrayGlobal.Add("sada") Commented Oct 28, 2021 at 12:09
  • This worked. Thanks a lot! @MathiasR.Jessen Commented Oct 28, 2021 at 12:14

1 Answer 1

1

PowerShell will allow you to write to non-local variables from within a class method body if you explicitly specify the variables scope:

$logArrayGlobal = [System.Collections.ArrayList]::new()

class LogManager {
    $logArrayClass = [System.Collections.ArrayList]::new()

    LogManager()
    {
        $global:logArrayGlobal.Add("sada")
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.