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
1
Question by headkit · Aug 24, 2010 at 04:00 PM · arraylistmultidimarray

c# string[][] to Arraylist problems

Hi folks!

i want to convert a multidimensional array "string[][] theArray" into an ArrayList and then read out one of the partial arrays "string[] theStringArray".

the problem is that i get the error that i want to convert an object into a string[].

my code is

void myFunction(int cnt) { ArrayList tmpList = ArrayToArrayList(theArray); int counter = cnt/2;

 while (counter-- >= 1)
 {
     int lengthOfList = tmpList.Count;
     int rndM = Random.Range(0,  lengthOfList - 1);
     string[] tmpPair = tmpList[rndM];   // <--- HERE I GET THE ERROR !!!
     tmpList.RemoveAt(rndM);
     //...
 }

}

ArrayList ArrayToArrayList(string[][] theArray) { ArrayList theList = new ArrayList(); for (int i = 0; i < theArray.Length; i++){ string[] tmp = theArray[i]; theList.Add(tmp); } return theList; }

so whats my problem (besides that I am new to c#)? thanx!

Comment
Add comment · Show 1
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 headkit · Aug 24, 2010 at 04:09 PM 0
Share

i forgot to mention that theArray is "private static string[][] theArray"

1 Reply

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

Answer by Mike 3 · Aug 24, 2010 at 04:08 PM

You need to be casting to string[] when you grab something from the arraylist:

string[] tmpPair = (string[])tmpList[rndM];

Ideally you'd modify the code to use List instead though, that way the code should be cleaner and you won't need to cast everywhere

List<string[]> tmpList = ArrayToList(theArray); string[] tmpPair = tmpList[rndM]; //will just work

List<string[]> ArrayToList(string[][] theArray) { List<string[]> theList = new List<string[]>(); //rest is the same

You'll need to have

using System.Collections.Generic;

at the top for that to work

Comment
Add comment · Show 5 · 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 headkit · Aug 25, 2010 at 07:54 AM 0
Share

great! thank you. a lot to learn for me, again...

avatar image headkit · Aug 25, 2010 at 08:19 AM 0
Share

hm, but when I then try to add the randomly chosen list-entry to another List ("anotherListDeclaredOutside.Add(tmpPair);") in the while-loop, I get the error "NullReferenceException: Object reference not set to an instance of an object"...

avatar image Mike 3 · Aug 25, 2010 at 12:39 PM 0
Share

I can't immediately see why that'd be, it could be the bug you have with the range though. You need to use Random.Range(0, lengthOfList) ins$$anonymous$$d of the one you have with -1, the top value for Range with an int isn't included already

avatar image headkit · Aug 25, 2010 at 03:51 PM 0
Share

nope, thats not the one... :-(

avatar image Mike 3 · Aug 25, 2010 at 05:23 PM 0
Share

I think you'll have to post the rest of the code, it sounds like it's just a case of the other list not being instantiated

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

No one has followed this question yet.

Related Questions

ArrayList Transform type! 2 Answers

C# ArrayList access from other script 1 Answer

Problem with multi prefabs collision 1 Answer

how to change a variable from? an object in this array with a gui.button 0 Answers

Adding Waypoints using a List 2 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