Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. about arraylist

about arraylist

Scheduled Pinned Locked Moved C#
question
9 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    loscarlitos
    wrote on last edited by
    #1

    I got a question. i am new in this things so How do you get to the properties of een instance in an arraylist. And it can be different kind of objects in the arraylist.

    P 1 Reply Last reply
    0
    • L loscarlitos

      I got a question. i am new in this things so How do you get to the properties of een instance in an arraylist. And it can be different kind of objects in the arraylist.

      P Offline
      P Offline
      Pradipta Basu
      wrote on last edited by
      #2

      I cannot get your first question. Arraylist can hold any kind of object. But for a particular instance, it should hold the same kind of object, ie., if you are keeping a string, you have to keep only strings, or if it is integer, you have to keep only integer.

      Pradipta Basu

      L 1 Reply Last reply
      0
      • P Pradipta Basu

        I cannot get your first question. Arraylist can hold any kind of object. But for a particular instance, it should hold the same kind of object, ie., if you are keeping a string, you have to keep only strings, or if it is integer, you have to keep only integer.

        Pradipta Basu

        L Offline
        L Offline
        loscarlitos
        wrote on last edited by
        #3

        example object motor() object window() Arraylist carParts carParts.add(motor) carParts.add(window) now i wanna get to the properties of the objects motor and winow in carParts. I wanna change value in those objects.

        L 1 Reply Last reply
        0
        • L loscarlitos

          example object motor() object window() Arraylist carParts carParts.add(motor) carParts.add(window) now i wanna get to the properties of the objects motor and winow in carParts. I wanna change value in those objects.

          L Offline
          L Offline
          Larantz
          wrote on last edited by
          #4

          foreach(object part in carParts)
          {
          if(part is motor)
          (part as motor).Cylinders = 12;

          if(part is window)
          (part as window).Electric = true;
          }

          Cast it to access the properties. Or store the object to a new variable. For instance:

          Motor newMotor = null;

          if(part is Motor)
          newMotor = (part as Motor);

          if(newMotor != null)
          {
          //access properties via newMotor object
          }

          -Larantz-

          for those about to code, we salute you
          http://www.tellus-software.com

          D 1 Reply Last reply
          0
          • L Larantz

            foreach(object part in carParts)
            {
            if(part is motor)
            (part as motor).Cylinders = 12;

            if(part is window)
            (part as window).Electric = true;
            }

            Cast it to access the properties. Or store the object to a new variable. For instance:

            Motor newMotor = null;

            if(part is Motor)
            newMotor = (part as Motor);

            if(newMotor != null)
            {
            //access properties via newMotor object
            }

            -Larantz-

            for those about to code, we salute you
            http://www.tellus-software.com

            D Offline
            D Offline
            DavidNohejl
            wrote on last edited by
            #5

            or, without looking up type of object twice:

            Motor newMotor = null;

            if((newMotor = part as Motor) != null)
            {
            //access properties via newMotor object
            }


            "Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus

            J 1 Reply Last reply
            0
            • D DavidNohejl

              or, without looking up type of object twice:

              Motor newMotor = null;

              if((newMotor = part as Motor) != null)
              {
              //access properties via newMotor object
              }


              "Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus

              J Offline
              J Offline
              J4amieC
              wrote on last edited by
              #6

              or, without the confusing assignment-and-cast-plus-equality-in-one // newMotor is null if myArrayList[0] is not of type Motor Motor newMotor = myArrayList[0] as Motor; if(newMotor != null) { }

              --- How to get answers to your questions[^]

              D 1 Reply Last reply
              0
              • J J4amieC

                or, without the confusing assignment-and-cast-plus-equality-in-one // newMotor is null if myArrayList[0] is not of type Motor Motor newMotor = myArrayList[0] as Motor; if(newMotor != null) { }

                --- How to get answers to your questions[^]

                D Offline
                D Offline
                DavidNohejl
                wrote on last edited by
                #7

                IMHO it's just about as confusing as ternary operator. btw, I don't see why would you comment what standart C# operator as does?? Oh wait, maybe because from

                ... some lines...
                if(newMotor != null)
                {
                
                } 
                

                it's not immediately clear that you are not testing for null, but for type of object...


                "Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus

                J 1 Reply Last reply
                0
                • D DavidNohejl

                  IMHO it's just about as confusing as ternary operator. btw, I don't see why would you comment what standart C# operator as does?? Oh wait, maybe because from

                  ... some lines...
                  if(newMotor != null)
                  {
                  
                  } 
                  

                  it's not immediately clear that you are not testing for null, but for type of object...


                  "Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus

                  J Offline
                  J Offline
                  J4amieC
                  wrote on last edited by
                  #8

                  AS is just a no-exception cast. If the cast object is not of the casted type it returns null. Perfect if you want to cast and read a property.

                  --- How to get answers to your questions[^]

                  D 1 Reply Last reply
                  0
                  • J J4amieC

                    AS is just a no-exception cast. If the cast object is not of the casted type it returns null. Perfect if you want to cast and read a property.

                    --- How to get answers to your questions[^]

                    D Offline
                    D Offline
                    DavidNohejl
                    wrote on last edited by
                    #9

                    CP ate my response :mad: I know what as *operator* does, thank you. However I am missing your point.


                    "Throughout human history, we have been dependent on machines to survive. Fate, it seems, is not without a sense of irony. " - Morpheus

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups