Simple Unboxing Question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Robert W.

    Simple Unboxing Question

    I have a situation where I have pairs of strings that I want to put into an
    ArrayList and then later pull out of it.

    To keep things simple I tried this to put them in:

    arrayList.Add(n ew object[] {stringA, stringB});


    That works fine. But I can't figure out how to unbox them into their
    constituent components. Might anyone know a way? Or am I forced to create
    simple class to define the object that the strings will initially be placed
    into?

    --
    Robert W.
    Vancouver, BC


  • Zeya

    #2
    Re: Simple Unboxing Question

    for ( int i = 0; i < arrayList.lengt h; i++ )
    {
    object TempObject[] = arrayList [ i ];
    for ( int j=0; j< TempObject.leng th; j++ )
    {
    console.writeli ne ( "i " + i + " j: " + (string)TempObj ect[ j ]
    );
    //or do whatever you want to do with each entry.
    }

    }

    Comment

    • Robert W.

      #3
      Re: Simple Unboxing Question

      But Zeya, this only pulls one string out of the object. There are actually
      two strings in every object element in the ArrayList.

      Or am I missing something?

      --
      Robert W.
      Vancouver, BC




      "Zeya" wrote:
      [color=blue]
      > for ( int i = 0; i < arrayList.lengt h; i++ )
      > {
      > object TempObject[] = arrayList [ i ];
      > for ( int j=0; j< TempObject.leng th; j++ )
      > {
      > console.writeli ne ( "i " + i + " j: " + (string)TempObj ect[ j ]
      > );
      > //or do whatever you want to do with each entry.
      > }
      >
      > }
      >
      >[/color]

      Comment

      • Bill Butler

        #4
        Re: Simple Unboxing Question

        "Robert W." <RobertW@discus sions.microsoft .com> wrote in message
        news:C3DECE83-636D-4AB5-9F86-FBBA22D8D742@mi crosoft.com...[color=blue]
        >I have a situation where I have pairs of strings that I want to put into an
        > ArrayList and then later pull out of it.
        >
        > To keep things simple I tried this to put them in:
        >
        > arrayList.Add(n ew object[] {stringA, stringB});
        >
        >
        > That works fine. But I can't figure out how to unbox them into their
        > constituent components. Might anyone know a way? Or am I forced to create
        > simple class to define the object that the strings will initially be placed
        > into?[/color]

        Hi,

        You don't need to use an Array of object. You can use a string array instead.

        Anyway... here is a sample program

        using System;
        using System.Collecti ons;

        class Test
        {
        public static void Main()
        {
        string[] pair1 = {"happy","Birth day"};
        string[] pair2 = {"Peanut butter","Jelly" };
        string[] pair3 = {"Good","Evil"} ;

        // Fill ArrayList
        ArrayList list = new ArrayList();
        list.Add(pair1) ;
        list.Add(pair2) ;
        list.Add(pair3) ;


        foreach(string[] pair in list)
        {
        Console.WriteLi ne("{0} AND {1}",pair[0], pair[1]);
        }
        }
        }

        OUTPUT
        ---------------------
        happy AND Birthday
        Peanut butter AND Jelly
        Good AND Evil

        Hope this helps
        Bill


        Comment

        • Zeya

          #5
          Re: Simple Unboxing Question

          There could be two reasons:
          1. If you did not correct my typo ( I am not sure if this even compiles
          this way)
          2. The loop index.

          Try following:

          for ( int i = 0; i < arrayList.lengt h; i++ )
          {
          object[] TempObject = arrayList [ i ]; &larr;
          for ( int j=0; j< TempObject.leng th; j++ ) &larr; change j <=
          TempObject.leng th
          {
          console.writeli ne ( "i " + i + " j: " + (string)TempObj ect[ j ]
          );
          //or do whatever you want to do with each entry.
          }

          HTH

          Comment

          • Ashura

            #6
            RE: Simple Unboxing Question

            wud this a better idea?

            using System.Collecti ons.Specialized ;

            //...
            NameValueCollec tion col = new NameValueCollec tion();
            col.Add("key1", "value1");
            string val = col["key1"];
            //...

            regards

            Comment

            • Robert W.

              #7
              Re: Simple Unboxing Question

              Bill,

              Yes, yes, simple but brilliant! I wish I had thought of that!

              Thanks!!


              --
              Robert W.
              Vancouver, BC




              "Bill Butler" wrote:
              [color=blue]
              > "Robert W." <RobertW@discus sions.microsoft .com> wrote in message
              > news:C3DECE83-636D-4AB5-9F86-FBBA22D8D742@mi crosoft.com...[color=green]
              > >I have a situation where I have pairs of strings that I want to put into an
              > > ArrayList and then later pull out of it.
              > >
              > > To keep things simple I tried this to put them in:
              > >
              > > arrayList.Add(n ew object[] {stringA, stringB});
              > >
              > >
              > > That works fine. But I can't figure out how to unbox them into their
              > > constituent components. Might anyone know a way? Or am I forced to create
              > > simple class to define the object that the strings will initially be placed
              > > into?[/color]
              >
              > Hi,
              >
              > You don't need to use an Array of object. You can use a string array instead.
              >
              > Anyway... here is a sample program
              >
              > using System;
              > using System.Collecti ons;
              >
              > class Test
              > {
              > public static void Main()
              > {
              > string[] pair1 = {"happy","Birth day"};
              > string[] pair2 = {"Peanut butter","Jelly" };
              > string[] pair3 = {"Good","Evil"} ;
              >
              > // Fill ArrayList
              > ArrayList list = new ArrayList();
              > list.Add(pair1) ;
              > list.Add(pair2) ;
              > list.Add(pair3) ;
              >
              >
              > foreach(string[] pair in list)
              > {
              > Console.WriteLi ne("{0} AND {1}",pair[0], pair[1]);
              > }
              > }
              > }
              >
              > OUTPUT
              > ---------------------
              > happy AND Birthday
              > Peanut butter AND Jelly
              > Good AND Evil
              >
              > Hope this helps
              > Bill
              >
              >
              >[/color]

              Comment

              Working...