473,388 Members | 1,326 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

C# ArrayList !!!!

35
HI,
I have created an array of ArrayList.
ArrayList arraylist=new ArrayList[7];
and them assin them the space
for( int index=0; index<arraylist.Length; index++ )
{
arraylist[ index ] = new ArrayList();
}
THen I save some values in it. Now I want to access index number 3 of arraylist[2] and for this purpose i have use the syntax
str=(( String )values[ g ].Item( j ).ToString());
but it gives the following error
'System.Collections.ArrayList' does not contain a definition for 'Item'

Kindly guide me in a right way
Oct 4 '07 #1
13 7475
Shashi Sadasivan
1,435 Expert 1GB
Could you also post an example of how you are storing values into it.
that might be the key.
cheers
Oct 4 '07 #2
newkhan
35
Could you also post an example of how you are storing values into it.
that might be the key.
cheers

I have use the code as under to store values in arraylist as under
for(int b=0;b<col_count;b++)
{
name=typ2[b].ToString(); // typ2 is another array which is working well
if (name.Equals("String")||name.Equals("VARCHAR")||na me.Equals("CHAR")||name.Equals("Text"))
{
string pick="ab "; // ab can be any string that should be added on arraylist
values[b].Add(pick);
}

Now I am reading values from arraylist as under
if(strsql.Equals("VARCHAR(100)"))
{
string str=(( String )values[ g ].Item( j ));
}

In java I have done the same thing with follwing
string str=(( String )values[ g ].get( j ));
and it doesn't give me any error.

But in case of C# it gives message that "arraylist has no definition of Item"

Please guide me in a right way I am very disturbed due to this problem.
Oct 4 '07 #3
r035198x
13,262 8TB
Wasn't your array declaration supposed to be

Expand|Select|Wrap|Line Numbers
  1. ArrayList[] list = new ArrayList[7];
Oct 4 '07 #4
Plater
7,872 Expert 4TB
Well that would be correct. ArrayLists don't have a property called "item"

declaration:
Expand|Select|Wrap|Line Numbers
  1. ArrayList[] mylist = new ArrayList[7];
  2. for (int i=0;i<mylist.Length;i++)
  3. {
  4.    mylist[i]=new ArrayList();
  5. }
  6.  
usage:
Expand|Select|Wrap|Line Numbers
  1. //this will look at the arraylist at index "2" of the regular array
  2. //and return the object stored at index "3" in that arraylist
  3. object o=((ArrayList)mylist[2])[3];
  4.  
Oct 4 '07 #5
Shashi Sadasivan
1,435 Expert 1GB
Yep, Plater is right about that one.

If you dont want to go through the trouble of typecasting, you can use a List<T>

cheers
Oct 4 '07 #6
newkhan
35
Thanks to all people who reply me and help me to solve my problem.
I Found the solution. Again thanks. This site is really helpful for sincere people.
Oct 6 '07 #7
newkhan
35
Hi ,

I use the c# code to store and extract values from the arraylist as under
Expand|Select|Wrap|Line Numbers
  1. ArrayList[] values=new ArrayList[col_count];
  2.             for( int index=0; index<col_count; index++ ) 
  3.             {
  4.                 values[ index ] = new ArrayList();    
  5.             }
  6. After assignning the space I started to store the sql table values in the list like
  7.  
  8. for(int a=0;a<tab_lenth;a++)
  9.  
  10.                     {
  11.                         for(int b=0;b<col_count;b++)
  12.                         {
  13.                             name=typ2[b].ToString();
  14. NOTE: type2 is another single arraylist and it works fine.
  15. if (name.Equals("String")||name.Equals("VARCHAR")||name.Equals("CHAR")||name.Equals("Text"))
  16.     {
  17.     string pick="";
  18.     pick=data.Rows[a][b].ToString();
  19.  
  20.     values[b].Add(pick);
  21.  
  22.     richTextBox1.Text= pick;
  23.  
  24.  
  25.             }
  26.                             else if (name.Equals("DOUBLE")||name.Equals("FLOAT"))
  27.     {
  28.  
  29.     values[b].Add( (Convert.ToDouble(data.Rows[a][b])));
  30.  
  31.     }
  32.                             else if (name.Equals("DECIMAL")||name.Equals("Int32"))
  33.                             {
  34.  
  35.         int num=Convert.ToInt32(data.Rows[a][b]);
  36.  
  37.         values[b].Add(num);
  38.  
  39.     }
  40.     }
  41. }    
  42.  
Problem: In all places above where i use the syntax
values[b].Add(num);
is not working well and not storing the values that it should have to do according to logic and syntax.
Oct 7 '07 #8
Shashi Sadasivan
1,435 Expert 1GB
To confirm on your code..
is "values" an arrayList?
if yes
is it an arraylist of arraylists?
or is it an arraylist of strings?

cheers
Oct 7 '07 #9
you can use DataSet!
Oct 7 '07 #10
newkhan
35
you can use DataSet!
Yes You are right it is arraylist of arryarslist. I want to store all double, int and string type values in it . Now guide me in right way.
Thanx
Oct 8 '07 #11
Plater
7,872 Expert 4TB
What is the error you are getting? It looks ok at first glance, although it seems rather messing when you could fill a single DataTable with everything in like 3lines of code
Oct 8 '07 #12
newkhan
35
What is the error you are getting? It looks ok at first glance, although it seems rather messing when you could fill a single DataTable with everything in like 3lines of code

Hi Plater,
It does not give any sytax error but when i wanted to check values in it then I came to know that there is no value in the arraylist. So, as a result there was not value in the sql server table that was created and no values were inserted through insert command. What did you mean by "you can use dataset". Plater please help me.
Oct 9 '07 #13
r035198x
13,262 8TB
.... What did you mean by "you can use dataset". Plater please help me.
Google it.
Oct 9 '07 #14

Sign in to post your reply or Sign up for a free account.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.