0

I am currently in the middle of trying to create a simple rpg C# game and I am stuck fairly early on in the map creation section.

I created a class for my locations:

public class Location
{
    private int ID;
    private string Name;
    public Location(int id, string name)
    {
        ID = id;
        Name = name;
    }
}

And I also created a method which fills a list with my locations. Outside in the main program area I created the list so it is accesible by everything:

List<Location> map = new List<Location>();
public void CreateMap()
    {
        Location start = new Location(1, "START");
        Location forest = new Location(2, "FOREST");
        Location city = new Location(3, "CITY");
        map.Add(start);
        map.Add(forest);
        map.Add(city);
    }
CreateMap();

However, I am stuck now, because I do not know how to access the parameters of my Location objects inside the list, as I only found how to access a string from a list on the internet, or very complicated and confusing answers that I did not at all understand. I wanted to somehow use a static class as I learned that they are usefull when we do not want to access the information inside the class, but I reckon I didn't quite grasp their concept.

tl;dr:I want to take my ID/Name of a specific object out of my list, but I do not know how.

Joseph
  • 13
  • 1
  • 5
  • 1
    Thats not what static classes are useful for. All you have to do is make the parameters `public` instead of `private`, or providing a public `getter`. You can't access private variables from outside of the class they are declared in. –  Dec 06 '19 at 18:50

3 Answers3

2

Change your Location class to use public properties:

public class Location
{
  public int Id {get;set;}
  public string Name {get;set;}
}

Your CreateMap would then look like this:

List<Location> map = CreateMap();

public List<Location> CreateMap()
{
  return new List<Location> {
    new Location {Id=1, Name="START"},
    new Location {Id=2, Name="FOREST"},
    new Location {Id=3, Name="CITY"}
  };
}

You can then reference Locations like this:

map.First(m=>m.Id==1).Name

Although, I would suspect you will be doing a lot of lookups by Id, and your List should more than likely be a Dictionary, or a simple array where the index is the Id instead, which will make location lookups much faster. In that case, you can easily convert to a dictionary like this:

Dictionary<int,Location> mapDict = CreateMap.ToDictionary(k=>k.Id, v=>v);

Then you can access location by id like this:

var location = mapDict[1];
var name = location.Name;
Robert McKee
  • 21,305
  • 1
  • 43
  • 57
0

If your Location class properties are public then you can iterate the list via foreach or for loop as shown below:

foreach(var location in map)
{
    var locationId = location.ID;
    var locationName = location.Name;
}

OR

for(var i; i< map.Count; i++)
{
    var locationId = map[i].ID;
    var locationName = map[i].Name;
}

If Location class properties are private, then you will need to add a public function to Location class to do the job. But it won't be an elegant solution:

    public class Location
    {
        private int ID;
        private string Name;
        public Location(int id, string name)
        {
            ID = id;
            Name = name;
        }
        public Tuple<int, string> GetById(int id)
        {
            if (id == this.ID)
            {
                return new Tuple<int, string>(this.ID, this.Name);
            }
            else
            {
                return new Tuple<int, string>(-1, "Not Found");
            }
        }
    }

and then outside you can access it like:

        var map = new List<Location>();
        map.Add(new Location(10, "A"));
        map.Add(new Location(20, "B"));
        map.Add(new Location(30, "C"));
        var bFound = false;
        Tuple<int, string> locationTuple;
        foreach (var location in map)
        {
            var byId = location.GetById(20);
            if (byId.Item1 > -1)
            {
                locationTuple = new Tuple<int, string>(byId.Item1, byId.Item2);
                break;
            }
        }
sam
  • 1,937
  • 1
  • 8
  • 14
0

Declare Location members as public so you can use List.Find(), for example:

Location start = map.Find(l => l.Name == "START");

Some useful information about List.Find() here.