Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I would suggest you to consider the possibility of more than one item of a certain type in a inventory list, i.e. two or more television sets instead of only one.</p> <p>Use a base class and derived classes:</p> <pre><code>public class InventoryItem { public string ItemType { get; set; } public string ItemName { get; set; } public int ItemAmount { get; set; } public int ItemACanHave { get; set; } public bool ItemClear { get; set; } public string ItemEffect { get; set; } public float ItemModifier { get; set; } public int ItemWeight { get; set; } } public class Radio : InventoryItem { } public class Television : InventoryItem { } // TODO: add your derived classes </code></pre> <p>Use a <code>List&lt;InventoryItem&gt;</code> to store the collection:</p> <pre><code>List&lt;InventoryItem&gt; InventoryItems = new List&lt;InventoryItem&gt;(); </code></pre> <p>Modify your method (don't forget to add exception handling, as sometimes you might get different input than the one you expected in the <code>args</code> object):</p> <pre><code>protected virtual bool OnAttempt_AddItem(object args) { // TODO: handle unboxing exceptions, size of the array etc // try { object[] arr = (object[])args; switch (arr[0].ToString().ToLower()) { // TODO: add other types (Radio etc) case "television": var tv = new Television(); tv.ItemType = (string)arr[0]; tv.ItemName = (string)arr[1]; tv.ItemAmount = (arr.Length == 2) ? (int)arr[2] : 1; tv.ItemACanHave = (arr.Length == 3) ? (int)arr[3] : 1; tv.ItemClear = (bool)arr[4]; tv.ItemEffect = (string)arr[5]; tv.ItemModifier = (float)arr[6]; tv.ItemWeight = (int)arr[7]; // enforce ability to have atleast 1 item of each type tv.ItemACanHave = Math.Max(1, tv.ItemACanHave); InventoryItems.Add(tv); break; default: var genericItem = new InventoryItem(); genericItem.ItemType = (string)arr[0]; genericItem.ItemName = (string)arr[1]; genericItem.ItemAmount = (arr.Length == 2) ? (int)arr[2] : 1; genericItem.ItemACanHave = (arr.Length == 3) ? (int)arr[3] : 1; genericItem.ItemClear = (bool)arr[4]; genericItem.ItemEffect = (string)arr[5]; genericItem.ItemModifier = (float)arr[6]; genericItem.ItemWeight = (int)arr[7]; // enforce ability to have atleast 1 item of each type genericItem.ItemACanHave = Math.Max(1, genericItem.ItemACanHave); InventoryItems.Add(genericItem); break; //handle other cases } return true; } catch (Exception ex) { // log the error return false; } } </code></pre> <p>Retrieve the filtered items like this:</p> <pre><code>var largeTvType = inventory.InventoryItems.OfType&lt;Television&gt;() // filter by type (or other criteria) .Where(tv =&gt; tv.ItemType == "large") // select only the property your interested in (in the case below // it will be always "television" because that's part of the // logic inside the OnAttempt_AddItem method's switch statement) .Select(tv =&gt; tv.ItemType); </code></pre> <p>Still, as ChrisWue suggested in his answer, if you know that your inventory lists will be very large, I'd recommend you to use a <code>Dictionary&lt;string, InventoryItem&gt;</code>, the <code>string</code> key being a unique inventory item identifier. It will be faster.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload