Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2012
    Location
    Denmark
    Posts
    66

    List max capacity

    Can I make a list and then say that it only can contain 9 strings for an example? Like if you want to make an inventory or something like that? If not, is there an other way of doing it

  2. #2
    Join Date
    Oct 2011
    Posts
    547
    You could use arrays with that concept because the number (size) is fixed.

    Easiest variant would be list (no ide when writing this so might have typo)

    Code:
    const int LIST_LIMIT = 9; // this would also be used for arrays
    
    List myList = new List<string>(); // I could use the constr to alloc capacity (but capacity is dynamic and would resize )
    PlaceOnList(string given);
    
    // This is to check the limit
    bool IsLimited(string given)
    {
    if(myList.count >= LIST_LIMIT) return true;
    return false;
    }
    
    // This is to place string in list
    void PlaceOnList(string given)
    {
    if(!IsLimited(given)) myList.Add(given);
    }
    You could also do new List<string>( LIST_LIMIT);
    But when the limit is reached the capacity will adjust from your default LIST_LIMIT to a bigger one. This is done for you to avoid the out of range problem.
    Last edited by florem; 04-01-2012 at 03:26 PM.

  3. #3
    Join Date
    Mar 2005
    Location
    Sydney, Australia
    Posts
    1,215
    Or have an int that you can increase/decrease should inventory size increase/decrease.

    int maxInventorySize.

    bool AddItemToInventory(Item item)
    {
    if (Inventory.Count >= MaxInventorySize)
    return false;
    else
    Inventory.Add(item);

    return true;
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •