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. How to make an array of a Class..?

How to make an array of a Class..?

Scheduled Pinned Locked Moved C#
csharpdata-structureshelptutorialquestion
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.
  • F Offline
    F Offline
    fmlove
    wrote on last edited by
    #1

    hi all! i'm new to c#. i have a class Item public class Item { ProductName = ""; Quantity = 0; } and want to make its array i.e Item[] please help me....

    C N D 3 Replies Last reply
    0
    • F fmlove

      hi all! i'm new to c#. i have a class Item public class Item { ProductName = ""; Quantity = 0; } and want to make its array i.e Item[] please help me....

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      Item[] myItems = new Item[numberOfElements];

      Recent blog posts: *SQL Server / Visual Studio install order *Installing SQL Server 2005 on Vista *Crazy Extension Methods Redux * Mixins My Blog

      F 1 Reply Last reply
      0
      • C Colin Angus Mackay

        Item[] myItems = new Item[numberOfElements];

        Recent blog posts: *SQL Server / Visual Studio install order *Installing SQL Server 2005 on Vista *Crazy Extension Methods Redux * Mixins My Blog

        F Offline
        F Offline
        fmlove
        wrote on last edited by
        #3

        thanks for reply.... please provide some details i also do this Item[] it = new Item[5]; but when i try to access its properties it can't it.ProductName // error occur here Object reference not set to an object...

        C 1 Reply Last reply
        0
        • F fmlove

          thanks for reply.... please provide some details i also do this Item[] it = new Item[5]; but when i try to access its properties it can't it.ProductName // error occur here Object reference not set to an object...

          C Offline
          C Offline
          Colin Angus Mackay
          wrote on last edited by
          #4

          fmlove wrote:

          it.ProductName // error occur here Object reference not set to an object

          That's correct. It isn't set to a reference of an object. All you have done is created an array. You haven't populated it with anything. To populate it you have to set each element with an instance of the class. For example:

          it[0] = new Item();

          Recent blog posts: *SQL Server / Visual Studio install order *Installing SQL Server 2005 on Vista *Crazy Extension Methods Redux * Mixins My Blog

          1 Reply Last reply
          0
          • F fmlove

            hi all! i'm new to c#. i have a class Item public class Item { ProductName = ""; Quantity = 0; } and want to make its array i.e Item[] please help me....

            N Offline
            N Offline
            N a v a n e e t h
            wrote on last edited by
            #5

            fmlove wrote:

            and want to make its array i.e Item[] please help me....

            Item[] item = { new Item(), new Item() };

            This creates array with two items.

            Navaneeth How to use google | Ask smart questions

            1 Reply Last reply
            0
            • F fmlove

              hi all! i'm new to c#. i have a class Item public class Item { ProductName = ""; Quantity = 0; } and want to make its array i.e Item[] please help me....

              D Offline
              D Offline
              DaveyM69
              wrote on last edited by
              #6

              Colin Angus Mackay has given you the correct answer to your question above. However, it's increasingly rare to need to have an array. Normally all we need is a collection in which case I'd recommend a generic list (from System.Collections.Generic).

              public List<item> itemCollection = new List<item>();

              You'll find that itemCollection has just about everything you'll need to manage the collection including the ToArray() method if you do need your items in an actual array. Edit: damn these html tags screwing up the generics!

              modified on Monday, October 6, 2008 8:35 AM

              realJSOPR 1 Reply Last reply
              0
              • D DaveyM69

                Colin Angus Mackay has given you the correct answer to your question above. However, it's increasingly rare to need to have an array. Normally all we need is a collection in which case I'd recommend a generic list (from System.Collections.Generic).

                public List<item> itemCollection = new List<item>();

                You'll find that itemCollection has just about everything you'll need to manage the collection including the ToArray() method if you do need your items in an actual array. Edit: damn these html tags screwing up the generics!

                modified on Monday, October 6, 2008 8:35 AM

                realJSOPR Offline
                realJSOPR Offline
                realJSOP
                wrote on last edited by
                #7

                It's actually:

                List<item> itemCollection = new List<item>();

                I'm sure that's how you originally typed it, but you forgot to turn the pointy brackets into escaped characters. :)

                "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                -----
                "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

                D 1 Reply Last reply
                0
                • realJSOPR realJSOP

                  It's actually:

                  List<item> itemCollection = new List<item>();

                  I'm sure that's how you originally typed it, but you forgot to turn the pointy brackets into escaped characters. :)

                  "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                  -----
                  "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

                  D Offline
                  D Offline
                  DaveyM69
                  wrote on last edited by
                  #8

                  I got there in the end after about 4 edits. Chris could do with the auto html tag thing having some common exclusions built in.

                  Dave
                  BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
                  Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

                  realJSOPR 1 Reply Last reply
                  0
                  • D DaveyM69

                    I got there in the end after about 4 edits. Chris could do with the auto html tag thing having some common exclusions built in.

                    Dave
                    BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
                    Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)

                    realJSOPR Offline
                    realJSOPR Offline
                    realJSOP
                    wrote on last edited by
                    #9

                    DaveyM69 wrote:

                    Chris could do with the auto html tag thing having some common exclusions built in.

                    It's been suggested.

                    "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                    -----
                    "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

                    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