简体   繁体   English

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

[英]Trouble passing value array into an Object array

I am trying to pass an array of values into an array of a table object so that I can write them to the database. 我试图将值的数组传递到表对象的数组中,以便可以将它们写入数据库。

My database looks like this -> 我的数据库看起来像这样->
tblCaseNotes tblCaseNotes
CaseNoteID | CaseNoteID | PersonId | PersonId | etc, etc 等等

tblCaseNotesContactType tblCaseNotesContactType
rowguid | rowguid | CaseNoteID | CaseNoteID | ContactTypeID ContactTypeID

tblMaintItems tblMaintItems
itemID | itemID | CategoryID 分类ID

The itemID from the Maint table is what is being written to the tblCaseNotesContactType along with the current CaseNoteID. 来自Maint表的itemID是与当前CaseNoteID一起写入tblCaseNotesContactType的内容。 There can be multiple ContactTypes per CaseNote. 每个CaseNote可以有多个ContactType。

What I have so far is an array of the Values for the CheckListBox ContactType created in my btnNew_Click Event: 到目前为止,我所拥有的是在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);

Which I then pass to my CurrentCaseNote object AddCNote method. 然后将其传递给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);

It is then passed to the DAL to be written to the local database: 然后将其传递到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();
}

It is giving me a NUllReferenceException was unhandled error on this line --> cTypeToAdd[0].CaseNoteID = caseNoteToAdd.CaseNoteID; 这给了我NUllReferenceException在此行上未处理的错误-> cTypeToAdd[0].CaseNoteID = caseNoteToAdd.CaseNoteID;
Is it because I am only working with the [0]? 是因为我只使用[0]吗? I just did that to simplify my testing of this. 我只是这样做以简化我对此的测试。 When I step through the code my value array is correct and there is a Guid for caseNoteToAdd.CasNoteID 当我单步执行代码时,我的值数组是正确的,并且有caseNoteToAdd.CasNoteID的Guid

Can someone give me a few pointers on where I am going wrong here and what might be causing the error? 有人可以给我一些关于我在哪里出问题的提示以及可能导致错误的原因吗? As you can tell from my code I am new to this and I am learning on the fly. 从您的代码中可以看出,我是新手,而且我正在实时学习。

Thanks, 谢谢,

~P 〜P

The problem is that you've created an array of reference types, but not populated it. 问题是您创建了一个引用类型数组,但没有填充它。

In other words, after this line: 换句话说,在此行之后:

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

you've got an array of null references - the value of each element is null. 您有一个空引用数组-每个元素的值为空。

You then need to write: 然后,您需要编写:

cTypeToAdd[0] = new tblCaseNotesContactType();

(or a similar statement) before you can start changing its properties. (或类似的语句),然后才能开始更改其属性。

An alternative would be to use an object initializer and do it in one statement (after creating the array): 一种替代方法是使用对象初始化程序,并在一个语句中创建它(在创建数组之后):

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