繁体   English   中英

无法将值数组传递给对象数组

[英]Trouble passing value array into an Object array

我试图将值的数组传递到表对象的数组中,以便可以将它们写入数据库。

我的数据库看起来像这样->
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);

然后将其传递给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);

然后将其传递到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();
}

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

有人可以给我一些关于我在哪里出问题的提示以及可能导致错误的原因吗? 从您的代码中可以看出,我是新手,而且我正在实时学习。

谢谢,

〜P

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

换句话说,在此行之后:

tblCaseNotesContactType[] cTypeToAdd = new tblCaseNotesContactType[cTypes.Length];

您有一个空引用数组-每个元素的值为空。

然后,您需要编写:

cTypeToAdd[0] = new tblCaseNotesContactType();

(或类似的语句),然后才能开始更改其属性。

一种替代方法是使用对象初始化程序,并在一个语句中创建它(在创建数组之后):

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM