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. Best way to declaring multiple objects

Best way to declaring multiple objects

Scheduled Pinned Locked Moved C#
csharpsysadmindata-structuresquestion
5 Posts 3 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.
  • R Offline
    R Offline
    rfresh
    wrote on last edited by
    #1

    I'm new to C# and have been reading up on Classes. In my small app, I want to instantiate a class called 'backupJob'. There are two operationType members: local to remote server and syncing both ways. I want to create an object for one op type and then create another object for the other type. In the demos I have seen they always show this: Car myCar1 = new Car(); Car myCar2 = new Car(); I may end up with more than two backup jobs so I'm thinking there must be a better way to manage the object names besides myCar1 and myCar2, etc. I will want to delete an object as well as create more objects so is it best to keep track of these objects in an Array or possibly a Collection? Thanks...

    A OriginalGriffO 2 Replies Last reply
    0
    • R rfresh

      I'm new to C# and have been reading up on Classes. In my small app, I want to instantiate a class called 'backupJob'. There are two operationType members: local to remote server and syncing both ways. I want to create an object for one op type and then create another object for the other type. In the demos I have seen they always show this: Car myCar1 = new Car(); Car myCar2 = new Car(); I may end up with more than two backup jobs so I'm thinking there must be a better way to manage the object names besides myCar1 and myCar2, etc. I will want to delete an object as well as create more objects so is it best to keep track of these objects in an Array or possibly a Collection? Thanks...

      A Offline
      A Offline
      Abhinav S
      wrote on last edited by
      #2

      Create and maintain objects in a collection (list maybe). You can add and remove objects as required from within the list.

      Apps - Color Analyzer | Arctic | XKCD | Sound Meter | Speed Dial

      1 Reply Last reply
      0
      • R rfresh

        I'm new to C# and have been reading up on Classes. In my small app, I want to instantiate a class called 'backupJob'. There are two operationType members: local to remote server and syncing both ways. I want to create an object for one op type and then create another object for the other type. In the demos I have seen they always show this: Car myCar1 = new Car(); Car myCar2 = new Car(); I may end up with more than two backup jobs so I'm thinking there must be a better way to manage the object names besides myCar1 and myCar2, etc. I will want to delete an object as well as create more objects so is it best to keep track of these objects in an Array or possibly a Collection? Thanks...

        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #3

        As Abhinav suggests, a collection is the easiest way to go: declare it as class level as a private List and you can do what you want with it:

        private List<Car> myCars = new List<Car>();
        ...
        Car car = new Car();
        car.Color = blue;
        car.Fuel = Fuels.Diesel;
        myCars.Add(car);
        ...
        myCars.Remove(car);
        ...
        foreach (Car car in myCars)
        {
        car.Respray(Color.Red);
        }

        This message is manufactured from fully recyclable noughts and ones. To recycle this message, please separate into two tidy piles, and take them to your nearest local recycling centre. Please note that in some areas noughts are always replaced with zeros by law, and many facilities cannot recycle zeroes - in this case, please bury them in your back garden and water frequently.

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        R 1 Reply Last reply
        0
        • OriginalGriffO OriginalGriff

          As Abhinav suggests, a collection is the easiest way to go: declare it as class level as a private List and you can do what you want with it:

          private List<Car> myCars = new List<Car>();
          ...
          Car car = new Car();
          car.Color = blue;
          car.Fuel = Fuels.Diesel;
          myCars.Add(car);
          ...
          myCars.Remove(car);
          ...
          foreach (Car car in myCars)
          {
          car.Respray(Color.Red);
          }

          This message is manufactured from fully recyclable noughts and ones. To recycle this message, please separate into two tidy piles, and take them to your nearest local recycling centre. Please note that in some areas noughts are always replaced with zeros by law, and many facilities cannot recycle zeroes - in this case, please bury them in your back garden and water frequently.

          R Offline
          R Offline
          rfresh
          wrote on last edited by
          #4

          Thanks for the help...!!!

          OriginalGriffO 1 Reply Last reply
          0
          • R rfresh

            Thanks for the help...!!!

            OriginalGriffO Offline
            OriginalGriffO Offline
            OriginalGriff
            wrote on last edited by
            #5

            You're welcome!

            This message is manufactured from fully recyclable noughts and ones. To recycle this message, please separate into two tidy piles, and take them to your nearest local recycling centre. Please note that in some areas noughts are always replaced with zeros by law, and many facilities cannot recycle zeroes - in this case, please bury them in your back garden and water frequently.

            "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
            "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

            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