Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Fexception · Dec 12, 2015 at 12:58 PM · c#dictionaryprint

Pulling and printing a random entry from a dictionary of a list of strings. C#.

Hi all. I have a dictionary of a list of strings. Each key can have more than 1 value associated with it. My problem is, when I use the following code to print out a random entry, I don't get what I asked for:

alt text

 void PullRandom() {
         System.Random rand = new System.Random();
         string word;
         word = dbCopy.ElementAt(rand.Next(0, dbCopy.Count)).Value.ToString();
         print(word);
     }

EDIT: After playing around with this a little bit, I got the following output with this code (and yes, the dictionary key is in Russian):

alt text

 void PullRandom() {
         StringBuilder builder = new StringBuilder();
         System.Random rand = new System.Random();
         string word;
         word = dbCopy.ElementAt(rand.Next(0, dbCopy.Count)).ToString();
         builder = builder.Append(word);
         print(builder);
         
     }

This is fine, however. It looks like it wants to add the list of values following the key.. I don't need them combined like that and I don't see where that is occurring if I am only grabbing the key.

So. What I don't understand is:

  1. Why does ToString() not work in this case?

  2. How can I get this to print out the actual string instead of whatever else it is trying to do?

  3. How can I print out the value separately from the key instead of them being combined in the same line?

capture.png (3.7 kB)
newcapture.png (4.0 kB)
Comment
Add comment · Show 9
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 Masterio · Dec 12, 2015 at 01:52 PM 0
Share

define a dbCopy

avatar image Fexception Masterio · Dec 12, 2015 at 01:55 PM 0
Share

that is my dictionary of a list of strings:

 Dictionary<string, List<string>> dbCopy = new Dictionary<string, List<string>>();
avatar image Masterio · Dec 12, 2015 at 02:13 PM 0
Share

int this line:

  word = dbCopy.ElementAt(rand.Next(0, dbCopy.Count)).ToString();

You converts to string a whole List from dbCopy value.

avatar image Fexception Masterio · Dec 12, 2015 at 02:53 PM 0
Share

Thanks, I actually just noticed that, I've been trying to update as I go along. But what about question #3?

avatar image Fexception Fexception · Dec 12, 2015 at 02:53 PM 0
Share

I'll update what I've been doing in a sec.

avatar image Masterio · Dec 12, 2015 at 02:42 PM 0
Share

Look into the code. I think it should solve your problem.

avatar image Masterio · Dec 12, 2015 at 03:44 PM 0
Share

For prints values from the List:

use simple foreach loop

after this line: List list = dbCopy[random$$anonymous$$ey];

add this :

foreach(string s in list) { print(s); }

avatar image Fexception Masterio · Dec 13, 2015 at 10:54 AM 0
Share

Thanks, I have one last problem. Printing the items out works, but I want each item added to a new List however I get the following error when I call PullRandom()

NullReferenceException: Object reference not set to an instance of an object TextChanger.PullRandom () (at Assets/Scripts/TextChanger.cs:83)

I don't see where there is an object reference needed. And for some reason, I had to set newList to null to make a compiler error go away, but then it shows up in Unity anyways. Why didn't I have to do this with randomListValues?

 void PullRandom() {
         List<string> randomListValues;
         List<string> newList = null;
         System.Random rand = new System.Random();
         int randNum;
         string word;
 
             for(int i = 0; i < 5; i++) {
                 randNum = rand.Next(0, dbCopy.Count);
                 word = dbCopy.ElementAt(randNum).$$anonymous$$ey;
                 randomListValues = dbCopy.ElementAt(randNum).Value;
 
                 for (int y = 0; y < randomListValues.Count; y++) {
                     string newWord = randomListValues[y];
                     newList.Add(newWord);
                     print(newWord);
                 }
                 
             }
     }
avatar image Fexception Masterio · Dec 15, 2015 at 10:50 AM 0
Share

Bump, any ideas?

1 Reply

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

Answer by Masterio · Dec 15, 2015 at 09:26 PM

You can read about lists here:

http://www.dotnetperls.com/list

and about a dictioanries here:

http://www.dotnetperls.com/dictionary

PS: You can add some null checks if you are not sure something like:

 if (randomListValues == null) 
 {
     print("randomListValues is null");
 }

then you will find what element is null.

Comment
Add comment · 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

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

35 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 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 avatar image

Related Questions

Getting "object reference not set" error when creating a list dictionary. 1 Answer

Traffic Light State Machine Dictionary Issues 2 Answers

Overriding Start(), is this just bad code? 1 Answer

Problem referencing an int inside of a dictionary. 0 Answers

Dictionary creates copy, rather than references 1 Answer


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