麻烦将值数组传递给Object数组

Ref*_*din 0 c# arrays oop winforms

我试图将一个值数组传递给一个表对象的数组,以便我可以将它们写入数据库.

我的数据库看起来像这样 - >
tblCaseNotes
CaseNoteID | PersonId | 等等

tblCaseNotesContactType
rowguid | CaseNoteID | ContactTypeID

tblMaintItems
itemID | 类别ID

Maint表中的itemID是与当前CaseNoteID一起写入tblCaseNotesContactType的内容.每个CaseNote可以有多个ContactType.

到目前为止,我在btnNew_Click事件中创建的CheckListBox ContactType的值数组:

// Contact Type check list box
int cTypeCount = chkContactType.CheckedItems.Count;
int [] contactTypes = new int[cTypeCount];

// reusable generic counter for looping thru the check lists
int cMaintCounter = 0;

foreach (int checkedItem in chkContactType.CheckedIndices)
{
    contactTypes[cMaintCounter] = (int)chkContactType.GetItemValue(checkedItem);
    cMaintCounter++;
}
CurrentCaseNote.AddCNote(Program._CurrentPerson.PersonID, Convert.ToDecimal(tbxTimeSpentUnits.Text), chkIsCaseLog.Checked, Convert.ToDateTime(datContactDate.Text), memContactDetails.Text, contactTypes);
Run Code Online (Sandbox Code Playgroud)

然后我将其传递给我的CurrentCaseNote对象AddCNote方法.

public static void AddCNote(int personID, decimal tsUnits, bool isCaseLog, DateTime cDate, string cDetails, int[] cTypes)
{
    var caseNoteToAdd = new tblCaseNote
                            {
                                CaseNoteID = Guid.NewGuid(),
                                PersonID = personID,
                                TimeSpentUnits =tsUnits,
                                IsCaseLog =isCaseLog,
                                ContactDate =cDate,
                                ContactDetails =cDetails,
                                InsertDate = DateTime.Now,
                                InsertUser = Environment.UserName
                            };

    tblCaseNotesContactType[] cTypeToAdd = new tblCaseNotesContactType[cTypes.Length];
    cTypeToAdd[0].CaseNoteID = caseNoteToAdd.CaseNoteID;
    cTypeToAdd[0].ContactTypeID =cTypes[0];
    cTypeToAdd[0].rowguid = Guid.NewGuid();

    CaseNoteDAL.addCNote(caseNoteToAdd,cTypeToAdd);
Run Code Online (Sandbox Code Playgroud)

然后将其传递给DAL以写入本地数据库:

public static void addCNote(tblCaseNote caseNote, tblCaseNotesContactType[] cType)
{
    foreach (var type in cType)
    {
        caseNote.tblCaseNotesContactTypes.Add(type);               
    }
    dcCaseNotes.tblCaseNotes.InsertOnSubmit(caseNote);

    //dcCaseNotes.tblCaseNotes.InsertOnSubmit(caseNoteToAdd);
    dcCaseNotes.SubmitChanges();
}
Run Code Online (Sandbox Code Playgroud)

它给了我一个NUllReferenceException在这一行是未处理的错误 - > cTypeToAdd[0].CaseNoteID = caseNoteToAdd.CaseNoteID;
是因为我只使用[0]?我这样做是为了简化我的测试.当我单步执行代码时,我的值数组是正确的,并且有一个Guid forcaseNoteToAdd.CasNoteID

有人可以给我一些关于我在哪里出错的指示以及可能导致错误的原因吗?从你的代码中可以看出,我是新手,我正在学习.

谢谢,

〜p

Jon*_*eet 6

问题是您已经创建了一个引用类型数组,但没有填充它.

换句话说,在这一行之后:

tblCaseNotesContactType[] cTypeToAdd = new tblCaseNotesContactType[cTypes.Length];
Run Code Online (Sandbox Code Playgroud)

你有一个空引用数组 - 每个元素的值为null.

然后你需要写:

cTypeToAdd[0] = new tblCaseNotesContactType();
Run Code Online (Sandbox Code Playgroud)

(或类似的声明),然后才能开始更改其属性.

另一种方法是使用对象初始值设定项并在一个语句中完成(在创建数组之后):

cTypeToAdd[0] = new tblCaseNotesContactType
{
    CaseNoteID = caseNoteToAdd.CaseNoteID,
    ContactTypeID =cTypes[0],
    rowguid = Guid.NewGuid()
}:
Run Code Online (Sandbox Code Playgroud)