none
Loop through list of objects RRS feed

  • Question

  • Trying to loop through my list of objects for a drop system I'm trying to build.

    Base Drop Class:

     public class Base_Drop
        {
            /// <summary>
            /// Type of Item
            /// </summary>
            public string Drop_Type { get; set; }
            /// <summary>
            /// List: Item Name - Item Count
            /// </summary>
            public List<Item_Drop> Items { get; set; }
            /// <summary>
            /// Used for General Item Drop Table
            /// </summary>
            public int Dice_Roll { get; set; }
        }


    Item Drop Object:

     public class Item_Drop
        {
            public object Item { get; set; }
            public long Count { get; set; }
        }

    Function:

     public List<Item_Drop> Item_Drop_6(int Dice_Roll)
            {
                var items = new List<Item_Drop>();
                Random rnd = new Random();
                int dice = 0;
    
                try
                {
                    switch(Dice_Roll)
                    {
                        case 1:
                            _Drop.Item = new Ancient_Stone();
                            _Drop.Count = 1;
                            _Items.Add(_Drop);
                            break;
                        case 2:
                            _Drop.Item = new Polished_Ancient_Stone();
                            _Drop.Count = 1;
                            _Items.Add(_Drop);
                            break;
                        case 3:
                            _Drop.Item = new Ancient_Stone();
                            dice = rnd.Next(1, 6);
                            _Drop.Count = dice;
                            _Items.Add(_Drop);
                            break;
                        case 4:
                            _Drop.Item = new Polished_Ancient_Stone();
                            dice = rnd.Next(1, 6);
                            _Drop.Count = dice;
                            _Items.Add(_Drop);
                            break;
                        case 5:
                            _Drop.Item = Random_Rune();
                            _Drop.Count = 1;
                            _Items.Add(_Drop);
                            break;
                        case 6:
                            _Drop.Item = Random_Rune_Recipe();
                            _Drop.Count = 1;
                            _Items.Add(_Drop);
                            break;
                    }
                }
                catch(Exception ex)
                {
    
                }
    
                return items;
            }

    Trigger Event:

    private void btnTriggerEvent_Click(object sender, EventArgs e)
            {
                var items = new List<Item_Drop>();
                int dice = _core.Roll_D20();
    
                items = _rm_drop.Item_Drop_20(dice);
    
                foreach(object item in items)
                {
                   
                }
            }

    Trying to loop through and cant seem to figure out which object is being returned and how to set it etc. Their will be different objects being returned and I need to know which object is being returned etc. 

    Friday, March 22, 2019 7:39 PM

All replies

  • If all the items in your list are derived from Base_Drop, then your Item_Drop list should have "public Base_Drop Item" instead of "public object Item".  Then, you can add some kind of an ID member to Base_Drop that each derived class fills in.  Then,

    foreach( Base_Drop item in items )
    {
        switch( item.ID )
        {
            case id_AncientStone:
                AncientStone s = (AncientStone)item;
                ...


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Friday, March 22, 2019 8:05 PM
  • If other techniques do not work yet (virtual functions, for example), then try this too:

    foreach( Item_Drop item in items )

    {

       Ancient_Stone anst = item as Ancient_Stone;

       if( anst != null )

       {

          . . .

          continue;

       }

       Polished_Ancient_Stone pas = item as Polished_Ancient_Stone;

       if( pas != null )

       {

          . . .

          continue;

       }

       . . .

    }



    • Edited by Viorel_MVP Friday, March 22, 2019 8:41 PM
    Friday, March 22, 2019 8:40 PM