在代码showen bellow" Games"是一个" Ilist"。(游戏的Ilist)。 "首页"是Game类的属性,返回" team" - 这又是另一个具有Name属性的类。 Name返回字符串。所以如果我想将Name设置为某个字符串,我该如何访问Name的属性? 我试过Game.Home.Name =" string"但是不起作用
[root@fedora20 microservice]# export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig
[root@fedora20 microservice]# pkg-config --modversion libbson-1.0
1.4.2
[root@fedora20 microservice]# pkg-config --modversion libmongoc-1.0
1.4.2
[root@fedora20 microservice]# gcc -o connect connect.c $(pkg-config --cflags --libs libbson-1.0 libmongoc-1.0)
[root@fedora20 microservice]# ls
connect connect.c
答案 0 :(得分:0)
首先,这是令人困惑的混乱代码。
避免为可读代码重命名此类内容:
public Team Home { get; set; }
您的代码中存在多个错误(NullReference,CastingError,并且肯定会误解数据库的工作方式)以添加游戏:
db.Games.Add (new Game
{
Date = Date,
Home = HomeTeamId, // <-- Exception (string to Team class)
Visitor = VisitorTeamId, // <-- Property doesn't exists on Game class
HomeScore = HomeScore, // <--- Also not exists
VisitorScore = VisitorScore, // <--- missing
Park = ParkId, // <-- Exception (string to unknown class)
Attendance = Attendance // also not defined in your 'Game' Class
}
);
如何解开这个spagetti代码?简单干净的编码:
确保您所定义的所有必需属性(用于访问)并初始化(防止NullReference错误):
public class Game
{
public Game(string homeTeamId, string visitorTeamId)
{
// Default Constructor
GamePark = new Park();
HomeTeam = new Team(homeTeamId);
VisitorTeam = new Team(visitorTeamId);
}
// Reference Objects
public DateTime Date { get; set; }
public Park GamePark { get; set; }
public Team HomeTeam { get; set; }
public Team VisitorTeam { get; set; }
// Value Objects
public int HomeScore { get; set; }
public int VisitorScore { get; set; }
public string ParkId { get; set; }
public int Attendance { get; set; }
}
我不确定这门课程的目的是什么,但根据你的其他部分,它肯定需要一个Id属性。
public class Team
{
public Team(string teamId)
{
TeamId = teamId;
TeamsLeague = new League();
}
public string TeamId { get; set; }
public League TeamsLeague { get; set; }
public string Name { get; set; }
}
只需调用构造函数并为团队传递2个值:
public void Example()
{
Game game = new Game("Team1","Team2");
db.Games.Add(game);
...
}
我希望这个基本构造能够以正确的方式引导您。现在一切都应该是可访问和可写的。您甚至可以扩展类构造函数以设置更多参数(如分数)。
选择一些教程或寻找可以教你如何编码的朋友。现在你脑海中显然存在许多对编程的误解。