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