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. Declaring a arrays of objects dynamically

Declaring a arrays of objects dynamically

Scheduled Pinned Locked Moved C#
tutorial
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.
  • W Offline
    W Offline
    winpoorni
    wrote on last edited by
    #1

    Hi all, I need to create arrays of object dynamically. for example class Pesron; Person[] p=new Person[10]; it creates 10 objects ob type person. I don't know the exact number of objects. i have to specify it in run time Thanks n Regards, Ramya.R

    S D W J 4 Replies Last reply
    0
    • W winpoorni

      Hi all, I need to create arrays of object dynamically. for example class Pesron; Person[] p=new Person[10]; it creates 10 objects ob type person. I don't know the exact number of objects. i have to specify it in run time Thanks n Regards, Ramya.R

      S Offline
      S Offline
      Stefan Troschuetz
      wrote on last edited by
      #2

      Try the ArrayList class.


      www.troschuetz.de

      1 Reply Last reply
      0
      • W winpoorni

        Hi all, I need to create arrays of object dynamically. for example class Pesron; Person[] p=new Person[10]; it creates 10 objects ob type person. I don't know the exact number of objects. i have to specify it in run time Thanks n Regards, Ramya.R

        D Offline
        D Offline
        Divyang Mithaiwala
        wrote on last edited by
        #3

        Hello Ramya, Do you know link list data structure. It will allow you to insert any number of object.


        Divyang Mithaiwala System Engineer & Software Developer

        W 1 Reply Last reply
        0
        • W winpoorni

          Hi all, I need to create arrays of object dynamically. for example class Pesron; Person[] p=new Person[10]; it creates 10 objects ob type person. I don't know the exact number of objects. i have to specify it in run time Thanks n Regards, Ramya.R

          W Offline
          W Offline
          Wjousts
          wrote on last edited by
          #4

          Person[] p = new Person[n] Where n is a varible with the number that you need. Alternatively if it's the case that even at runtime you don't know how many you will need then use the ArrayList class.

          1 Reply Last reply
          0
          • W winpoorni

            Hi all, I need to create arrays of object dynamically. for example class Pesron; Person[] p=new Person[10]; it creates 10 objects ob type person. I don't know the exact number of objects. i have to specify it in run time Thanks n Regards, Ramya.R

            J Offline
            J Offline
            Judah Gabriel Himango
            wrote on last edited by
            #5

            A few others have replied here saying to use ArrayList. That is fine. If you're using .NET 2.0, you can use the System.Collections.Generic.List object to store your Person objects in a strongly-typed fashion (which ArrayList will not do). For example:

            using System.Collections.Generic;
            ...
            List< Person > persons = new List< Person >();
            persons.Add(somePerson);
            persons.Add(5); // compile-time error: persons can only contain Person objects. This is something ArrayList will NOT give you, hence the benefit of using List

            -- modified at 10:39 Wednesday 8th March, 2006

            W 1 Reply Last reply
            0
            • D Divyang Mithaiwala

              Hello Ramya, Do you know link list data structure. It will allow you to insert any number of object.


              Divyang Mithaiwala System Engineer & Software Developer

              W Offline
              W Offline
              winpoorni
              wrote on last edited by
              #6

              Can i use the link list concept in C#.It needs pointer to denote address.. Thanks n Regards, Ramya.R

              D 1 Reply Last reply
              0
              • J Judah Gabriel Himango

                A few others have replied here saying to use ArrayList. That is fine. If you're using .NET 2.0, you can use the System.Collections.Generic.List object to store your Person objects in a strongly-typed fashion (which ArrayList will not do). For example:

                using System.Collections.Generic;
                ...
                List< Person > persons = new List< Person >();
                persons.Add(somePerson);
                persons.Add(5); // compile-time error: persons can only contain Person objects. This is something ArrayList will NOT give you, hence the benefit of using List

                -- modified at 10:39 Wednesday 8th March, 2006

                W Offline
                W Offline
                winpoorni
                wrote on last edited by
                #7

                Thanks Judah Himango .. Your coding was very useful to me. Thanks a lot Thanks n Regards, Ramya.R

                1 Reply Last reply
                0
                • W winpoorni

                  Can i use the link list concept in C#.It needs pointer to denote address.. Thanks n Regards, Ramya.R

                  D Offline
                  D Offline
                  Divyang Mithaiwala
                  wrote on last edited by
                  #8

                  Do you know that when u declare any object it will just declare reference not allocate memory for that. Now you can use this concept for link list.


                  Divyang Mithaiwala System Engineer & Software Developer

                  W 1 Reply Last reply
                  0
                  • D Divyang Mithaiwala

                    Do you know that when u declare any object it will just declare reference not allocate memory for that. Now you can use this concept for link list.


                    Divyang Mithaiwala System Engineer & Software Developer

                    W Offline
                    W Offline
                    winpoorni
                    wrote on last edited by
                    #9

                    yeah! I know that concept. I'll try to implement that. Thanks a Lot Divyang Mithaiwala!!!! Thanks n Regards, Ramya.R

                    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