ASP.Net C#将项目添加到锯齿状数组 [英] ASP.Net c# adding items to jagged array

查看:48
本文介绍了ASP.Net C#将项目添加到锯齿状数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将项目添加到锯齿状数组中,数据是从datarowview中提取的,我有以下代码

im trying to add items to jagged array's, the data is being pulled from a datarowview, i have the following code

foreach (DataRowView answer in AnswersInQuestion)
{
    answersJArray[index] = new string[noOfAnswersInQuestion];
    answersJArray[index][j] = answer["ChoiceText"].ToString();
    j++;
}

可以很好地添加第一个项目,但是当第二个项目放入第一个项目时,它将再次设置为null.例如,这是第一次,这就是数组的样子

the first item gets added in fine, but when the 2nd item is put in the first item is set to null again. so for example the first time round this is what the array will look like

arr[0][0] = answer 1
arr[0][1] = null
arr[0][2] = null
arr[0][3] = null

第二遍,数组看起来像

arr[0][0] = null
arr[0][1] = answer 2
arr[0][2] = null
arr[0][3] = null

有人可以帮我这个忙!

谢谢

推荐答案

您的构造函数每次都被调用(因此第一项设置为null).将字符串数组构造函数放在for-each循环之外(也许在自己的循环中.

Your constructor is being called each time (hence the first item being set to null). Put your string array constructor outside of your for-each loop (perhaps in its own loop.

这篇关于ASP.Net C#将项目添加到锯齿状数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆