C# C-如何在类的特定实例上设置ID
我对C很陌生,所以如果我问了一个愚蠢的问题,请原谅 这是我的代码:C# C-如何在类的特定实例上设置ID,c#,C#,我对C很陌生,所以如果我问了一个愚蠢的问题,请原谅 这是我的代码: public class SpellsTemplate { public int id; public string name; public int type; public float base_damage; public int buff; public float buff_debuff_time; public int healing; public f
public class SpellsTemplate
{
public int id;
public string name;
public int type;
public float base_damage;
public int buff;
public float buff_debuff_time;
public int healing;
public float base_healing;
public int class_specific;
public float cooldown;
public float cast_time;
public string icon;
public string game_prefab;
public float flying_speed;
public int chain;
public float chain_distance;
public int chain_max_targets;
}
public class SpellsHandler
{
public List<SpellsTemplate> spellsTemplate = new List<SpellsTemplate>();
.......
some other stuff here....
.......
spellsTemplate.Add(new SpellsTemplate()
{
id = Convert.ToInt32(mysqlReader["id"]),
name = mysqlReader["name"].ToString(),
type = Convert.ToInt32(mysqlReader["type"]),
base_damage = (float)mysqlReader["base_damage"],
buff = Convert.ToInt32(mysqlReader["buff"]),
buff_debuff_time = (float)mysqlReader["buff_debuff_time"],
healing = Convert.ToInt32(mysqlReader["healing"]),
base_healing = (float)mysqlReader["base_healing"],
class_specific = Convert.ToInt32(mysqlReader["class_specific"]),
cooldown = (float)mysqlReader["cooldown"],
cast_time = (float)mysqlReader["cast_time"],
icon = mysqlReader["icon"].ToString(),
game_prefab = mysqlReader["game_prefab"].ToString(),
flying_speed = (float)mysqlReader["flying_speed"],
chain = Convert.ToInt32(mysqlReader["chain"]),
chain_distance = (float)mysqlReader["chain_distance"],
chain_max_targets = Convert.ToInt32(mysqlReader["chain_max_targets"])
});
}
然后将类变量的数据放入
此外,如果这是可能的,我如何可以直接从没有foreach的所有其他实例中选择该实例
可能吗?在SpellsTemplate类上编写一个构造函数。这将强制任何实例化SpellsTemplate类的人提供某些值,例如ID
public class SpellsTemplate
{
public SpellsTemplate(int id)
{
this.Id = id;
}
// Make the setter for the ID property private, so it
// can only be set within this class (e.g. in the constructor).
public int Id { get; private set; }
// The rest of your properties go here.
}
构造函数是面向对象编程中非常重要的一部分-请:
编辑
关于第二个问题:
此外,如果这是可能的,我如何可以直接从没有foreach的所有其他实例中选择该实例
如果您只想使代码更干净,可以编写类似以下内容的代码:
int myId = 5;
SpellsTemplate mySpell = spellsTemplate.FirstOrDefault(spell => spell.Id == myId);
然而,这是在做一件事
如果您真正关心性能,则需要使用哈希表或字典之类的数据类型。您也可以使用字典,如下所示:
Dictionary<int, object> spellsTemplate = new Dictionary<int, object>();
spellsTemplate.add(Convert.ToInt32(mysqlReader["id"]), yourObject);
有没有办法在类的实例上设置ID?=>设置id值是此行应该做的事情:id=Convert.ToInt32mysqlReader[id],。这样不行吗?你想改一下吗?我不明白你的第一个问题到底是什么意思。关于第二种不使用foreach通过id快速选择项目的方法,有一个专门为该注释设计的Dictionary类:使用公共字段被认为是不好的做法。您应该将这些属性包装成可以在每个属性的末尾添加{get;set;}的属性,并将它们转换为自动属性。第二:不要将所有内容都读取或存储为字符串,在数据存储中使用本机类型,并在读取器中将其作为本机类型读回。创建一个构造函数,在SpellsTemplate类中接受int,然后设置id。对于第二个问题,LINQ是答案谢谢。看起来真不错。但是我如何才能选择类变量,比如像这样的一个base\u damage;
Dictionary<int, object> spellsTemplate = new Dictionary<int, object>();
spellsTemplate.add(Convert.ToInt32(mysqlReader["id"]), yourObject);
spellsTemplate[id];