public class Item
{
/*some variables */
public Item(){}
}
public class myItem : Item
{
public myItem(){/*some variables*/}
}
public class Inventory : List<Item>
{
public Inventory() {}
}
OK so I have two instances of inventory.
in inventoryA I have 5 instances of myItem.
I have drop code that subtracts 3 from the amount of myItem in inventoryA.
I need it to place 3 NEW instances of that item in inventoryB.
However I don't know what the type of that item is, because it is supposed to be able to be ANY item. It has to have the same properties as the item selected to be dropped.
Please help. I am banging my face into the desk.

1 answers
I'm not entirely clear what the problem is but, if it helps, you can tell what type of item you've got in one inventory and create an identical item in the other inventory with code such as this:
using System;
using System.Collections.Generic;
public class Item
{
/*some variables */
public Item(){}
}
public class myItem : Item
{
public decimal Amount;
public myItem Clone()
{
// shallow clone
return (myItem)MemberwiseClone();
}
public myItem(){/*some variables*/}
}
public class Inventory : List<Item>
{
public Inventory() {}
}
class Test
{
static void Main()
{
Inventory inventoryA = new Inventory();
Inventory inventoryB = new Inventory();
myItem mi = new myItem();
mi.Amount = 100m;
inventoryA.Add(mi);
Item it = new Item();
inventoryA.Add(it);
foreach(Item item in inventoryA)
{
if (item is myItem)
{
myItem oldItem = (myItem)item;
oldItem.Amount -= 3m;
myItem newItem = oldItem.Clone();
inventoryB.Add(newItem);
}
}
Console.WriteLine(((myItem)inventoryA[0]).Amount); // 97
Console.WriteLine(((myItem)inventoryB[0]).Amount); // 97
Console.WriteLine(inventoryA[0] == inventoryB[0]); // False
Console.ReadKey();
}
}
answered 2 years ago by:
17279
Yeah I guess I wasn't being clear enough, so I'm sorry for that. I should have said that my DropItem method was inside of my Item class, and a call to it is made from my Inventory class.
So inside the DropItem found in the Item class, what I have is:
for (int i = 0; i < mobile.inventory.Count; i++)
{
if (mobile.inventory.ElementAt(i).GetType() == this.GetType())
{
int amountLeft = inventory1.ElementAt(i).amount - i_Amount;
int amountToDrop = inventory1.ElementAt(i).amount - amountLeft;
inventory1.ElementAt(i).amount = amountLeft;
Item item = new Item();
//Cast item as type of this.
item.amount = amountToDrop;
inventory2.AddItem(item);
}
}
answered 2 years ago by:
121
for (int i = 0; i <inventory1.Count; i++)
{
if (inventory1.ElementAt(i).GetType() == this.GetType())
{
int amountLeft = inventory1.ElementAt(i).amount - i_Amount;
int amountToDrop = inventory1.ElementAt(i).amount - amountLeft;
inventory1.ElementAt(i).amount = amountLeft;
Item item = new Item();
//Cast item as type of this.
item.amount = amountToDrop;
inventory2.AddItem(item);
}
}
answered 2 years ago by:
121
Why don't you make the DropItem method in the Item class 'virtual' and then override it in each of the derived item classes?
That way, when you call DropItem() on an Item reference, the run-time type of Item will determine which method gets called and you won't need to worry about casting to the correct type in order to access the appropriate members.
answered 2 years ago by:
17279
That's not such a bad idea. I might drop the "cast it" idea and go with that. Thank you for your help!
answered 2 years ago by:
121
This post was imported from csharpfriends, if you have a similiar question please ask it again.
All previous members have been migrated, hope you enjoy the new platform!