![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Apr 2006
Location: Calgary, Alberta
Posts: 67
Rep Power: 3
![]() |
Problem with an ArrayList
I'm using an Array list to hold a bunch of struct items, but I can't seem to get an insertion going.
struct FullItem
{
public String name;
public int code;
public int quantity;
public double price;
public double weight;
}
. . .
private ArrayList Purchase = new ArrayList();
. . .
FullItem FullPurchase;
FullPurchase.code = purchase.Code;
FullPurchase.name = purchase.Name;
FullPurchase.price = purchase.Price;
if (purchase.ByWeight == true)
{
FullPurchase.weight = 100 / 1000; // add wieght input
FullPurchase.price = FullPurchase.weight * FullPurchase.price;
}
FullPurchase.quantity = 1; // add something for getting Q numbers
Purchase.Add(FullPurchase); //----this line has the error-------
. . .Oddly enough, I inserted a different sturct item into a different ArrayList, with no problem, and I can clearly access members of FullPurchase, but the add line gives me the following error, wherever I put it: Error 1 Use of unassigned local variable 'FullPurchase' Suggestions? |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
Join Date: Oct 2005
Posts: 134
Rep Power: 3
![]() |
Even though struct's are stored on the stack, they still need to be allocated and initialized with a call to new:
FullItem FullPurchase = new FullItem(); I'm surprised the errors didnt start on this line: FullPurchase.code = purchase.Code; |
|
|
|
|
|
#3 |
|
Programmer
Join Date: Apr 2006
Location: Calgary, Alberta
Posts: 67
Rep Power: 3
![]() |
Yeah, that did it. But It surprises me that I have another instance of this in a different program where I didn't use new and it worked fine
![]() |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|