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. creating Dynmaic Objects in C#

creating Dynmaic Objects in C#

Scheduled Pinned Locked Moved C#
tutorialcsharphelp
5 Posts 4 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.
  • A Offline
    A Offline
    Anil Kumar Arvapalli
    wrote on last edited by
    #1

    How to create dynamic objects in C# in Runtime. For example, I will list no of drives present in a System if it is 6 drives i have to create 6 Different objects can any one help me regarding this... string []drives=GetLogicalDrives(); for(int i=0;i

    OriginalGriffO 1 Reply Last reply
    0
    • A Anil Kumar Arvapalli

      How to create dynamic objects in C# in Runtime. For example, I will list no of drives present in a System if it is 6 drives i have to create 6 Different objects can any one help me regarding this... string []drives=GetLogicalDrives(); for(int i=0;i

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

      This is basic stuff, you should read a C# book as you will get a much more detailed explanation than we can put here (and probably easier to follow): In your example:

      string[] drives = GetLogicalDrives();

      Returns an array of strings, each containing a name (lets say) of a drive. Use a foreach loop, rather than a for loop - you don't need to know how many drives there are.

      foreach (string drive in drives)
      {
      DriveInfo di = new DriveInfo(drive);
      ... do something with the drive.
      }

      DriveInfo is a class which I invented to return info about a drive given its name. Each time you deal with a different drive from the drives array, you create a new instance of DriveInfo to handle it, with the new keyword. Does that answer your question?

      You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy

      "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

      A 1 Reply Last reply
      0
      • OriginalGriffO OriginalGriff

        This is basic stuff, you should read a C# book as you will get a much more detailed explanation than we can put here (and probably easier to follow): In your example:

        string[] drives = GetLogicalDrives();

        Returns an array of strings, each containing a name (lets say) of a drive. Use a foreach loop, rather than a for loop - you don't need to know how many drives there are.

        foreach (string drive in drives)
        {
        DriveInfo di = new DriveInfo(drive);
        ... do something with the drive.
        }

        DriveInfo is a class which I invented to return info about a drive given its name. Each time you deal with a different drive from the drives array, you create a new instance of DriveInfo to handle it, with the new keyword. Does that answer your question?

        You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy

        A Offline
        A Offline
        AspDotNetDev
        wrote on last edited by
        #3

        OriginalGriff wrote:

        DriveInfo is a class which I invented

        System.IO.DriveInfo :)

        [Forum Guidelines]

        OriginalGriffO 1 Reply Last reply
        0
        • A AspDotNetDev

          OriginalGriff wrote:

          DriveInfo is a class which I invented

          System.IO.DriveInfo :)

          [Forum Guidelines]

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

          Yes, yes, I know! :laugh: Just using it as a sensible name for a class the OP might want to implement to go with the "drives" array they already had. I dunno, .NET has taken too many of the good names already, why can't they have used DICFMSTOMYLH instead, and left all the good ones to us? They used to in the early days! :omg: DriveInfoClassFromMicroSoftTOMakeYourLifeHarder, in case you were wondering...

          You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy

          "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

          G 1 Reply Last reply
          0
          • OriginalGriffO OriginalGriff

            Yes, yes, I know! :laugh: Just using it as a sensible name for a class the OP might want to implement to go with the "drives" array they already had. I dunno, .NET has taken too many of the good names already, why can't they have used DICFMSTOMYLH instead, and left all the good ones to us? They used to in the early days! :omg: DriveInfoClassFromMicroSoftTOMakeYourLifeHarder, in case you were wondering...

            You should never use standby on an elephant. It always crashes when you lift the ears. - Mark Wallace C/C++ (I dont see a huge difference between them, and the 'benefits' of C++ are questionable, who needs inheritance when you have copy and paste) - fat_boy

            G Offline
            G Offline
            Greg Chelstowski
            wrote on last edited by
            #5

            OriginalGriff wrote:

            DriveInfoClassFromMicroSoftTOMakeYourLifeHarder, in case you were wondering...

            ROFL! I was wondering, I knew there had to be some method to that madness ;)

            var question = (_2b || !(_2b));

            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