我正在开发一个将用户添加到 Active Directory 中的组的应用程序,它有两个类:ADUser 和 Student,它们继承自 ADUser。
public class ADUser
property UserID as string
property MemberOf as new List(Of String)
(etc)
end class
public class Student inherits ADUser
property subjects as new list(Of Subject) 'Subject is a class of multiple strings
end class
我试图将主题名称从主题列表复制到 MemberOf 列表,这会导致 NullReferenceException(对象引用未设置为对象的实例)。以下代码在 me.MemberOf.Add 行上生成错误:
Function ConvertSubjectsToGroupMembership() As Boolean
Try
For Each s As Subject In Subjects
Me.MemberOf.Add(String.Format("t.{0}", s.ShortName).ToString)
Next
Return True
Catch ex As Exception
Return False
End Try
End Function
我对继承的理解是,在创建 Student 对象时应该创建 MemberOf 列表,但情况似乎并非如此。
任何帮助表示赞赏。
编辑:我设法通过修改函数来创建一个列表(字符串)来解决这个问题,然后将其复制到 MemberOf 属性:
Function ConvertSubjectsToGroupMembership() As Boolean
Dim SubjectShortNameList As New List(Of String)
For Each s As Subject In Subjects
SubjectShortNameList.Add(String.Format("t.{0}", s.ShortName).ToString)
Next
Try
MemberOf = SubjectShortNameList
Return True
Catch ex As Exception
Return False
End Try
End Function
它有效,但我确信有更好的方法来做到这一点。我期待阅读其他人的答案/意见!