Initializing Dictionary Arrays

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 2 years and 1 month old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
B Daring
Posts: 90
Last visit: Mon Feb 05, 2024 3:00 pm
Answers: 2
Has voted: 1 time

Initializing Dictionary Arrays

Post by B Daring »

I am trying to understand "System.Collections.Specialized.OrderedDictionary" I get the concept I just don't understand how to inialize the array with no values. So this is what I have, I am reading an ini file that has info like this

Code: Select all

[User Information]
LastName=The Dog
DisplayName=Goofy
Then after reading the file I have

$LastName = $UserContent["User Information"]["LastName"]

But lets say the file is not created and I want to create it with this information,

$UserContent["User Information"]["LastName"] = "The Dog"

but, when I do that I get, can't index into null array error. Which I understand because the $UserContent is basically $null and not an array at the point of reading the file. So if the file is not existent how do I setup [User Information] and nothing else?
I have tried

$UserContent["User Information"] = New-Object System.Collections.Specialized.OrderedDictionary([System.StringComparer]::OrdinalIgnoreCase)
But still get the can't index into null array

So, basically if I do have a file with just [User Information] in it the code works fine.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Initializing Dictionary Arrays

Post by jvierra »

It is impossible to understand your question since you seem to be asking more than one question and merging them together in some strange way.

You keep referring to "file". What does a file have to do with an object collection?
User avatar
B Daring
Posts: 90
Last visit: Mon Feb 05, 2024 3:00 pm
Answers: 2
Has voted: 1 time

Re: Initializing Dictionary Arrays

Post by B Daring »

Sorry about that. Lets try again.
I have a file with this in it

Code: Select all

[User Information]
LastName=The Dog
DisplayName=Goofy
Then after reading the file I have

$LastName = $UserContent["User Information"]["LastName"]

But lets say I want to update the last name,

$UserContent["User Information"]["LastName"] = "The Dog"

Then save it to file, this also works. But, if if there is not a file then doing this

$UserContent["User Information"]["LastName"] = "The Dog"

Ends with an error - cannot index into null array. The point I am trying to get to is if there is no file to read I want to create one with the $UserContent in it. But if I can't populate the array how do I ad it to a file?

For clarification I am using these functions
https://github.com/lipkau/PsIni
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Initializing Dictionary Arrays

Post by jvierra »

You have to create the node before you assign it. I suggest reading through the examples and doing them until you understand the developers' intentions. You are dealing with very old code that was started by "The Scripting Guy" which is a blog that has been out of service for years. The Net Fr4amework has changed dramatically since then. If you try all examples it wil help you understand if the old code still works.

To use the dictionary directly you have to use the "Add" method to add an entry at the hierarchy level you want and at the root dictionary if the root key doesn't exist. At the root you would add a new dictionary with its key then add entries to the new dictionary.
  1. $od = [System.Collections.Specialized.OrderedDictionary]::new()
  2. $od.Add('NewEntry',[System.Collections.Specialized.OrderedDictionary]::new())
  3. $od['NewEntry'].Add('MyEntry','Tom Thumbs Pudding Pie')
User avatar
B Daring
Posts: 90
Last visit: Mon Feb 05, 2024 3:00 pm
Answers: 2
Has voted: 1 time

Re: Initializing Dictionary Arrays

Post by B Daring »

Thanks jvierra,

That's what I needed. Yes, I realize the code is old, but I was handed this code from a predecessor and just trying to work out a bug. We aren't quite using it like the author suggests so using his examples won't work for us. I suppose I will have to tackle the code head on if .NET gets to far ahead. But for now this will have to do.
This topic is 2 years and 1 month old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked