Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by LoganPark · Oct 29, 2013 at 04:15 PM · c#foreachdictionarypublicprotection

C# Dictionary protection level when foreach-ing

Here's the error:

 error CS0122: `System.Collections.Generic.KeyValuePair<string,int>.Value' is inaccessible due to its protection level

Here's the code that triggers it:

     using UnityEngine;
     using System;
     using System.Collections;
     using System.Collections.Generic;
     using System.IO;

     public class HSInventoryManager : MonoBehaviour {
     public Dictionary<string, int> InventoryItemsDictionary = new Dictionary<string, int>();
     
     public void Start (){
         //First, populate dictionary contents with items the player MIGHT have.
         
         InventoryItemsDictionary.Add("Hydrogen", 0);
         InventoryItemsDictionary.Add("Helium", 0);
         InventoryItemsDictionary.Add("Lithium", 0);
         // And so forth
         
         bool PlayerPrefsNeedUpdate = false;
 
         //Second, update the dictionary contents with savegame data wherever it exists, create entries otherwise
         foreach( KeyValuePair<string, int> InventoryItemDictionaryEntry in InventoryItemsDictionary ) {
             if( PlayerPrefs.HasKey( InventoryItemDictionaryEntry.Key )) {
                 InventoryItemDictionaryEntry.Value = PlayerPrefs.GetInt( InventoryItemDictionaryEntry.Key );
             } else {
                 PlayerPrefs.SetInt(InventoryItemDictionaryEntry.Key, InventoryItemDictionaryEntry.Value);
                 PlayerPrefsNeedUpdate = true;
             }
         }
         
         // Third, update savegame data with the fresh inventory levels, if needed.
         if ( PlayerPrefsNeedUpdate ) {
             PlayerPrefs.Save();
         }
     }
 }

I have declared everything public that I can identify... so why would there be a protection level problem?

Thank you for your guidance.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Dave-Carlile · Oct 29, 2013 at 04:18 PM

The Value property is read only.

http://msdn.microsoft.com/en-us/library/ms224761.aspx

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image LoganPark · Oct 29, 2013 at 08:49 PM 0
Share

Well, I learned something today. Thank you.

I guess this sends me back to using multiple parallel arrays or strided lists for read/write lists with text keys functionality?

Blarg.

avatar image OP_toss · Oct 29, 2013 at 08:58 PM 4
Share

Not at all! Just use the key to index the dictionary.

 foreach( $$anonymous$$eyValuePair<string, int> InventoryItemDictionaryEntry in InventoryItemsDictionary ) {
          if( PlayerPrefs.Has$$anonymous$$ey( InventoryItemDictionaryEntry.$$anonymous$$ey )) {
           InventoryItemsDictionary[InventoryItemDictionaryEntry.$$anonymous$$ey] = PlayerPrefs.GetInt( InventoryItemDictionaryEntry.$$anonymous$$ey );
          }
avatar image LoganPark · Oct 29, 2013 at 09:03 PM 1
Share

@Dave Carlile's answer was the one I deserved. @OP_toss' answer was the one I needed. :)

OP_toss++

EDIT: your solution indeed appears to have worked. In the co$$anonymous$$g inevitable apocalypse, I shall endeavor to ensure that my $$anonymous$$ions do not mangle and devour your corpse. Have a nice day.

avatar image OP_toss · Oct 30, 2013 at 03:16 AM 0
Share

Happy to be of service, good sir.

avatar image OP_toss · Nov 04, 2013 at 10:34 PM 0
Share

Actually, I forgot about one thing in my answer, dictionaries will become out of sync if you attempt to alter them in a foreach loop...

Ins$$anonymous$$d you have to either copy the keys out first, and iterate over those, or maintain a list of changes to which you apply in a second loop.

See this post for more details and examples

Sorry for the confusion...

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can you iterate through a dictionary and change every value? 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

C# multiple key/value pairs of various types in a single line of code...? 1 Answer

Not updating value in foreach loop OnGUI() 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges