C# ArrayList !!!!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • newkhan
    New Member
    • Oct 2007
    • 35

    #1

    C# ArrayList !!!!

    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.Collect ions.ArrayList' does not contain a definition for 'Item'

    Kindly guide me in a right way
  • Shashi Sadasivan
    Recognized Expert Top Contributor
    • Aug 2007
    • 1435

    #2
    Could you also post an example of how you are storing values into it.
    that might be the key.
    cheers

    Comment

    • newkhan
      New Member
      • Oct 2007
      • 35

      #3
      Originally posted by Shashi Sadasivan
      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("S tring")||name.E quals("VARCHAR" )||name.Equals( "CHAR")||name.E quals("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.Equal s("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.
      Last edited by Frinavale; Oct 9 '07, 01:04 PM. Reason: fixed [quote] tag

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Wasn't your array declaration supposed to be

        [CODE=css]ArrayList[] list = new ArrayList[7];[/CODE]

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          Well that would be correct. ArrayLists don't have a property called "item"

          declaration:
          Code:
          ArrayList[] mylist = new ArrayList[7];
          for (int i=0;i<mylist.Length;i++)
          {
             mylist[i]=new ArrayList();
          }
          usage:
          Code:
          //this will look at the arraylist at index "2" of the regular array
          //and return the object stored at index "3" in that arraylist
          object o=((ArrayList)mylist[2])[3];

          Comment

          • Shashi Sadasivan
            Recognized Expert Top Contributor
            • Aug 2007
            • 1435

            #6
            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

            Comment

            • newkhan
              New Member
              • Oct 2007
              • 35

              #7
              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.

              Comment

              • newkhan
                New Member
                • Oct 2007
                • 35

                #8
                C# ArrayList Problem Again !!!!

                Hi ,

                I use the c# code to store and extract values from the arraylist as under[code=cpp]
                ArrayList[] values=new ArrayList[col_count];
                for( int index=0; index<col_count ; index++ )
                {
                values[ index ] = new ArrayList();
                }
                After assignning the space I started to store the sql table values in the list like

                for(int a=0;a<tab_lenth ;a++)

                {
                for(int b=0;b<col_count ;b++)
                {
                name=typ2[b].ToString();
                NOTE: type2 is another single arraylist and it works fine.
                if (name.Equals("S tring")||name.E quals("VARCHAR" )||name.Equals( "CHAR")||name.E quals("Text"))
                {
                string pick="";
                pick=data.Rows[a][b].ToString();

                values[b].Add(pick);

                richTextBox1.Te xt= pick;


                }
                else if (name.Equals("D OUBLE")||name.E quals("FLOAT"))
                {

                values[b].Add( (Convert.ToDoub le(data.Rows[a][b])));

                }
                else if (name.Equals("D ECIMAL")||name. Equals("Int32") )
                {

                int num=Convert.ToI nt32(data.Rows[a][b]);

                values[b].Add(num);

                }
                }
                }
                [/code]
                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.

                Comment

                • Shashi Sadasivan
                  Recognized Expert Top Contributor
                  • Aug 2007
                  • 1435

                  #9
                  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

                  Comment

                  • chenxsh2615
                    New Member
                    • Jun 2006
                    • 3

                    #10
                    you can use DataSet!

                    Comment

                    • newkhan
                      New Member
                      • Oct 2007
                      • 35

                      #11
                      Originally posted by chenxsh2615
                      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

                      Comment

                      • Plater
                        Recognized Expert Expert
                        • Apr 2007
                        • 7872

                        #12
                        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

                        Comment

                        • newkhan
                          New Member
                          • Oct 2007
                          • 35

                          #13
                          Originally posted by Plater
                          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.

                          Comment

                          • r035198x
                            MVP
                            • Sep 2006
                            • 13225

                            #14
                            Originally posted by newkhan
                            .... What did you mean by "you can use dataset". Plater please help me.
                            Google it.

                            Comment

                            Working...