Solved

How to Assign ArrayList element to Integer array

Posted on 2003-10-31
11
1,109 Views
Last Modified: 2012-06-22
Hi,
  The code is given below

             ArrayList aSequenceList= new ArrayList();

             (The above arraylist contain list like 23,45,67,89) I wants to assign these elements to integer array. For this the code which i written.

           int iArrSeq;

           for (int i=0; i< aSequenceList.Count; i++)
           {
      iArrSeq[i]=aSequenceList[i];    Not working
               iArrSeq[i]=int.parse(aSequenceList[i]); Not Working
           }
           gives error like "Cannot implicitly convert type 'object' to 'int'"
 
           Please give me any suggestion or solution.

Thanks,
           
0
Comment
Question by:manjiri_j
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 5

Expert Comment

by:tgannetts
ID: 9657051
Try the following line

iArrSeq = int.Parse(aSequenceList[i].ToString());

Tom
0
 
LVL 10

Expert Comment

by:smegghead
ID: 9657601
Change your int.parse to Convert.ToInt32

Otherwise, a tidier solution, which prevents you from having to go through the whole array is:-

iArrSeq=(int[])aSequenceList.ToArray(typeof(Int32));

0
 
LVL 20

Expert Comment

by:TheAvenger
ID: 9661354
Here is the code fixed:

ArrayList aSequenceList= new ArrayList();

int[] iArrSeq = new int[aSequenceList.Count];

for (int i=0; i< aSequenceList.Count; i++)
{
     iArrSeq[i]=(int)aSequenceList[i];
}
0
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
LVL 3

Expert Comment

by:Infinity88
ID: 9664239
int[] iArr = (int[]) myList.ToArray(typeof(int))
0
 
LVL 10

Expert Comment

by:smegghead
ID: 9665382
infinity88... how is that different to my suggestion ???
0
 
LVL 3

Expert Comment

by:Infinity88
ID: 9666943
You used Int32 -- why refer directly to the framework class for a primative type?

I dont expect points if thats what you're worried about.
0
 
LVL 10

Expert Comment

by:smegghead
ID: 9670768
Ok, fair enough, but int & Int32 are the same thing. 'int' is just an alias for the 'framework class' System.Int32

for some reason, when I'm casting something, I always use 'int' - maybe it's just from my c++ days.
0
 
LVL 3

Accepted Solution

by:
Infinity88 earned 125 total points
ID: 9672522
Agreed that it doesnt matter -- but for some reason it bugs me when I see ppl referring directly to the primative classes. Even when they use "String" instead of "string". For one, the syntax highlighter doesnt catch it -- and secondly, it makes the code look more like VB.
0
 
LVL 10

Expert Comment

by:smegghead
ID: 9677366
wow, very diplomatic manjiri_j.

1. You gave a grade 'b' for a solution to your problem.. why not an 'a' ?? If there is a problem with the solution provided, please state what it is, the experts here put their time and effort into trying to help you, the least you can do is post a brief comment explaining the reason for the low grade.

2. You have only graded 1 out of your 10 open questions.

3. Did you actually read the last few posts where Infinity88 agreed that his solution was the same as mine ?? Usually you accept the first correct answer.

Smg.
0
 

Author Comment

by:manjiri_j
ID: 9678256
Hi,

      I very sorry to say about grading. That should be grade "A" only. I really appreciate your efforts and thanks for that. And i hope you co-operate me, same in the future.

Thanks,
0
 

Author Comment

by:manjiri_j
ID: 9678259
Hi,

      I >am< very sorry to say about grading. That should be grade "A" only. I really appreciate your efforts and thanks for that. And i hope you co-operate me, same in the future.

Thanks,
0

Featured Post

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question