我有一個類,名爲Permissions
,它有幾個子類(和一些屬性),Group
和Group.Permission
。現在「x.Groups = list」works but「x.Groups.Add(listItem)」引發空引用異常?
,我CAN做以下
Permissions u = new Permissions();
u.userId = (Guid)user.ProviderUserKey;
List<int> groups = getGroupsForUserId(u.userId.ToString());
List<Permissions.Group> groupItems = new List<Permissions.Group>();
foreach (int g in groups)
{
Permissions.Group groupItem = new Permissions.Group();
groupItem.group_id = g;
groupItem.Records = getRecordsForGroupId(g);
groupItem.Permissions = getPermissionsForGroupId(g);
groupItems.Add(groupItem);
}
u.Groups = groupItems;
不過,我CAN NOT做到這一點:
Permissions u = new Permissions();
u.userId = (Guid)user.ProviderUserKey;
List<int> groups = getGroupsForUserId(u.userId.ToString());
foreach (int g in groups)
{
Permissions.Group groupItem = new Permissions.Group();
groupItem.group_id = g;
groupItem.Records = getRecordsForGroupId(g);
groupItem.Permissions = getPermissionsForGroupId(g);
u.Groups.Add(groupItem);
}
我的問題,爲什麼呢?
我創建了一個新的Permissions.Group
對象,增加值,然後把它添加到的Groups
的u
對象的名單。在我看來,應該工作,但是我認爲我沒有在我的課堂上佈線?我的課只是一個屬性列表property_name { get; set; }
所以真的沒什麼異乎尋常的。
嘗試後,我上線u.Groups.Add(groupItem);
向我們展示'Permissions'類。 – birryree
那麼,哪個表達式(*。*)**的* left *是** null?每次都是一樣的故事......過於本土化。 (此外,還有什麼本質「OOP」在這個問題上。) – 2012-05-23 16:57:41
@pst:我覺得你的意思向左^^ – Falanwe