简体   繁体   中英

PowerShell Class; property “System.Collections.Specialized.OrderedDictionary”; how to add dictionary entry

(As I have searched the web quite intensively for an answer and didn't get any hint in the right direction, I allow myself to ask you the following question:)

I have a PowerShell class like this:

class settings
{
    [string]$CFG_Name
    [string]$CFG_Template_Path
    [string]$CFG_Target_Path
    [System.Collections.Specialized.OrderedDictionary]$ConfigSettings
}

I then instantiate this class and want to use it like this:

$CurrentSettings = New-Object settings    <-- works fine
$CurrentSettings.CFG_Name = 'MPD'    <-- works fine
$CurrentSettings.CFG_Template_Path = $CFG_Template_MPD_Path    <-- works fine
$CurrentSettings.CFG_Target_Path = $CFG_MPD_Path    <-- works fine

$CurrentSettings.ConfigSettings.Add("TID",$EFT_TID)    <-- doesn't work

The line above throws: You cannot call a method on a null-valued expression.

According to this page: Documentation from Microsoft the add method is supported.

Why can't I just use this property like I can use for example $CFG_Name?

How would I add dictionary entries to this property?

The solution is the following:

Before the adding of the first dictionary entry, you have to initialize the property like this:

$CurrentSettings.ConfigSettings = New-Object System.Collections.Specialized.OrderedDictionary

Then you can add the entry:

$CurrentSettings.ConfigSettings.Add("TID",$EFT_TID)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM