[英]C# - List of Objects - Acces an object's parameter
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.我目前正在尝试创建一个简单的 rpg C# 游戏,但我很早就陷入了地图创建部分。
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.但是,我现在卡住了,因为我不知道如何访问列表中我的 Location 对象的参数,因为我只找到了如何访问互联网上列表中的字符串,或者我所做的非常复杂和令人困惑的答案完全不明白。 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. tl;dr:我想从列表中删除特定对象的 ID/名称,但我不知道如何操作。
Change your Location class to use public properties:更改您的 Location 类以使用公共属性:
public class Location
{
public int Id {get;set;}
public string Name {get;set;}
}
Your CreateMap would then look like this:您的 CreateMap 将如下所示:
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.虽然,我怀疑您将通过 Id 进行大量查找,并且您的 List 很可能是一个字典,或者一个简单的数组,其中索引是 Id,这将使位置查找更快。 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:然后您可以通过 id 访问位置,如下所示:
var location = mapDict[1];
var name = location.Name;
If your Location class properties are public then you can iterate the list via foreach or for loop as shown below:如果您的 Location 类属性是公开的,那么您可以通过 foreach 或 for 循环迭代列表,如下所示:
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.如果 Location 类属性是私有的,那么您需要向 Location 类添加一个公共函数来完成这项工作。 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;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.