C#-对象列表-访问对象';s参数 我目前正试图创建一个简单的RPG C游戏,我在地图制作部分很早就被卡住了。
我为我的位置创建了一个类:C#-对象列表-访问对象';s参数 我目前正试图创建一个简单的RPG C游戏,我在地图制作部分很早就被卡住了。,c#,.net,winforms,C#,.net,Winforms,我为我的位置创建了一个类: public class Location { private int ID; private string Name; public Location(int id, string name) { ID = id; Name = name; } } 我还创建了一个方法,用我的位置填充列表。在主程序区域之外,我创建了列表,以便所有人都可以访问它: List<Location> map
public class Location
{
private int ID;
private string Name;
public Location(int id, string name)
{
ID = id;
Name = name;
}
}
我还创建了一个方法,用我的位置填充列表。在主程序区域之外,我创建了列表,以便所有人都可以访问它:
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();
List map=newlist();
public void CreateMap()
{
位置开始=新位置(1,“开始”);
位置森林=新位置(2,“森林”);
地点城市=新地点(3,“城市”);
map.Add(开始);
添加地图(森林);
添加地图(城市);
}
CreateMap();
然而,我现在陷入了困境,因为我不知道如何访问列表中我的位置对象的参数,因为我只发现如何访问internet上列表中的字符串,或者我根本不理解的非常复杂和混乱的答案。我想以某种方式使用静态类,因为我知道当我们不想访问类中的信息时,它们是有用的,但我认为我没有完全理解它们的概念
tl;dr:我想从列表中删除特定对象的ID/名称,但我不知道如何操作。如果您的位置类属性是公共的,则可以通过foreach或for循环迭代列表,如下所示:
foreach(var location in map)
{
var locationId = location.ID;
var locationName = location.Name;
}
或
for(var i;i
如果Location类属性是私有的,那么您将需要向Location类添加一个公共函数来执行此任务。但这不是一个优雅的解决方案:
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");
}
}
}
公共类位置
{
私有int-ID;
私有字符串名称;
公共位置(整数id、字符串名称)
{
ID=ID;
名称=名称;
}
公共元组GetById(int-id)
{
if(id==this.id)
{
返回新元组(this.ID,this.Name);
}
其他的
{
返回新元组(-1,“未找到”);
}
}
}
然后在外面你可以像这样访问它:
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;
}
}
var-map=newlist();
地图.增补(新位置(10,“A”);
地图.增补(新位置(20,“B”);
地图.增补(新位置(30,“C”);
var bFound=假;
元组位置元组;
foreach(地图中的变量位置)
{
var byId=location.GetById(20);
如果(byId.Item1>-1)
{
locationTuple=新的元组(byId.Item1,byId.Item2);
打破
}
}
如果您的Location类属性是公共的,那么您可以通过foreach或for循环迭代列表,如下所示:
foreach(var location in map)
{
var locationId = location.ID;
var locationName = location.Name;
}
或
for(var i;i
如果Location类属性是私有的,那么您将需要向Location类添加一个公共函数来执行此任务。但这不是一个优雅的解决方案:
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");
}
}
}
公共类位置
{
私有int-ID;
私有字符串名称;
公共位置(整数id、字符串名称)
{
ID=ID;
名称=名称;
}
公共元组GetById(int-id)
{
if(id==this.id)
{
返回新元组(this.ID,this.Name);
}
其他的
{
返回新元组(-1,“未找到”);
}
}
}
然后在外面你可以像这样访问它:
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;
}
}
var-map=newlist();
地图.增补(新位置(10,“A”);
地图.增补(新位置(20,“B”);
地图.增补(新位置(30,“C”);
var bFound=假;
元组位置元组;
foreach(地图中的变量位置)
{
var byId=location.GetById(20);
如果(byId.Item1>-1)
{
locationTuple=新的元组(byId.Item1,byId.Item2);
打破
}
}
将位置
成员声明为公共
,以便您可以使用List.Find()
,例如:
Location start = map.Find(l => l.Name == "START");
有关
List.Find()
的一些有用信息将位置
成员声明为公共
,以便您可以使用List.Find()
,例如:
Location start = map.Find(l => l.Name == "START");
有关
List.Find()
的一些有用信息更改Location类以使用公共属性:
public class Location
{
public int Id {get;set;}
public string Name {get;set;}
}
您的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"}
};
}
map.First(m=>m.Id==1).Name
Dictionary<int,Location> mapDict = CreateMap.ToDictionary(k=>k.Id, v=>v);
var location = mapDict[1];
var name = location.Name;
尽管如此,我还是怀疑您将通过Id进行大量查找,并且您的列表很可能是一个字典,或者是一个索引为Id的简单数组,这将使位置查找更快。在这种情况下,您可以轻松地转换为以下词典:
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"}
};
}
map.First(m=>m.Id==1).Name
Dictionary<int,Location> mapDict = CreateMap.ToDictionary(k=>k.Id, v=>v);
var location = mapDict[1];
var name = location.Name;
将Location类更改为使用公共属性:
public class Location
{
public int Id {get;set;}
public string Name {get;set;}
}
您的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"}
};
}
map.First(m=>m.Id==1).Name
Dictionary<int,Location> mapDict = CreateMap.ToDictionary(k=>k.Id, v=>v);
var location = mapDict[1];
var name = location.Name;
尽管如此,我还是怀疑您将通过Id进行大量查找,并且您的列表很可能是一个字典,或者是一个索引为Id的简单数组,这将使位置查找更快。在这种情况下,您可以轻松地转换为以下词典:
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"}
};
}
map.First(m=>m.Id==1).Name
Dictionary<int,Location> mapDict = CreateMap.ToDictionary(k=>k.Id, v=>v);
var location = mapDict[1];
var name = location.Name;
这不是静态类的用途。您所要做的就是将参数
设置为public
而不是private
,或者提供一个publicgetter
。不能从声明私有变量的类之外访问它们。这不是静态类的用途。您所要做的就是将参数设置为public
而不是private
,或者提供一个publicgetter
。您无法从声明私有变量的类外部访问它们。这将不起作用,因为他的Location类没有公共字段或属性。正确。谢谢你纠正我。我以前没有注意到。这不起作用,因为他的位置类