Hi every body, i want to get a data from List<T> so;

class Npc : IDisposable 
{
    public short id;
    public float fX;
    public float fZ;
    public float fY;
}

internal List<Npc> m_arNpcArray = new List<Npc>();

I want to get data like this;
m_arNpcArray.GetData(int id);

GetData() {
 for(int i = 0; i < List.Count; i++) {
     if(List[i].id == id)
     {
         return List[i];
     }
 }
}

how can i declare m_arNpcArray.GetData ??

Edited 3 Years Ago by efe.ozyer

Methods have four parts: <access> <return type> <name> <parameters>, so

public Npc GetData(int id) {

Edited 3 Years Ago by Momerath

It looks like what you want is to extend List<T>. I've tried this and it works for me:

    // add this to be able to access the extension
    using MyExtensions;

    namespace MyExtensions
    {
        public static class My_Extensions
        {
            public static int GetData(this List<Npc> sender, short id)
            {

                for (int i = 0; i < (sender.Count); i++)
                {
                    if (sender[i].id == id)
                        return i;
                }
                return -1;
            }
            //The type is declared here.  I wasn't sure what you wanted to do with IDisposable
            //so I commented it out in order to compile
            public struct Npc //: IDisposable
            {
                public short id;
                public float fX;
                public float fZ;
                public float fY;


            }
        }
    }
//Your main program is here
namespace MainApp
{  
    class Program
    {
            static void Main()
            {
                //Make your list like this
                List<My_Extensions.Npc> m_arNpcArray = new List<My_Extensions.Npc>()
                { 
                    new My_Extensions.Npc{id = 1, fX = 10, fZ = 10, fY = 10}, 
                    new My_Extensions.Npc{id = 2, fX = 13, fZ = 16, fY = 18},
                    new My_Extensions.Npc{id = 3, fX = 15, fZ = 11, fY = 17}
                };

                //Find the id like this.

                int FindIndex = m_arNpcArray.GetData(3);
            }
       }
}

Edited 3 Years Ago by tinstaafl

This article has been dead for over six months. Start a new discussion instead.