1
\$\begingroup\$

Obviously, lists automatically create their own index. So basically I wouldn't need this function at all. But I figured it was safer in case my list index ever changed (e.g. adding a new item at the beginning of the list, would shift the index numbers for all the other list items).

public EmpireAgentView ShowEmpireView(int eIndex)
{

    foreach (EmpireAgentView e in EmpireViews)
    {
        if (e.EmpireIndex == eIndex)
        {
            Debug.Log("Showing Empire for Agent: " + e.AgentName);
            return e;
        }
    }

    Debug.LogError("No EmpireAgentView Found for that empire: " + eIndex);
    return new EmpireAgentView(9999);
}

So in this case, I added the property "EmpireIndex" to the EmpireAgentView object. So now, whenever I need to access a specific EmpireAgentView from the EmpireViews list, I would use ShowEmpireView(1) instead of EmpireViews[1].

Each EmpireAgentView object has unique information about the same Agent object. For example, where the Agent is currently located. If we have 4 empires, than there will be 4 EmpireAgentView objects in the Agent class. Each empire has their own idea of where the Agent is currently located, so I need to be able to access the specific empires data on this agent. Right now, I assign a simple int called EmpireIndex to each EmpireAgentView, so that I can make sure I got the right view for the right empire. If i simply used EmpireViews[3], and that list rearranged itself somehow, (like adding an EmpireAgentView object to the beginning of the list, presumably) than all that information would be mixed up for the empires.

Is that the best way of doing that? is there a better way of finding it? Or am I being TOO careful, and just need to not add new EmpireView's to the beginning of the list? (I'm new to C#, is there anything else that would change the index of a list item?)

\$\endgroup\$
1
\$\begingroup\$

When using a list, your way would be the best way to find the correct view within the list. It ensures that the correct view is found, no matter what index it happens to have in the list. This also elminates any problems that could appear, if an agent was put at the wrong index from the beginning.

The list index can be changed via multible operations. Generally it is pretty uncommon to add items in the beginnig of a list when used like this. I would worry more about entries being removed, as that would shift all following items.

If you end up with a lot of Empires, your lookup time would also start to rise, since in worst case, you would have to go through the whole list to find a view. To prevent this and get a constant lookup time, I would suggest using a Dictonary instead, with an int as the key, and the view as the value. Read more here C# Dictonary vs List lookup time

By using a dictonary, the lookup would be as simple as:

public EmpireAgentView ShowEmpireView(int eIndex)
{
    if(EmpireViews.ContainsKey(eIndex)){
        EmpireAgentView e = EmpireViews[eIndex];
        Debug.Log("Showing Empire for Agent: " + e.AgentName);
        return e;
    }

    Debug.LogError("No EmpireAgentView Found for that empire: " + eIndex);
    return new EmpireAgentView(9999);
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.