Dictionary(二)
这是问题:
各位好!我已用Dictionary<string,string>获取了数据,现在想将name对应的value赋给一个对象数组中,请问用C#如何写语句?
我写的代码如下:
public AlarmNotificationLoginInfo[] mappingByNotificationLoginInfo(Dictionary<string, string> dic)
{
AlarmNotificationLoginInfo[] anli = new AlarmNotificationLoginInfo[dic.Count];
int i = 0;
try
{
if (dic != null && dic.Count > 0)
{
foreach (KeyValuePair<string, string> kvp in dic)
{
if (i < dic.Count)
{
switch (kvp.Key)
{
case "username":
anli[i].Username = kvp.Value;
break;
case "password":
anli[i].Password = kvp.Value;
break;
default:
break;
}
}
}
i++;
}
}
catch (Exception e)
{
throw e;
}
return anli;
}
问题出现在标红的地方,抛出异常:未将对象引用设置到对象的实例。
我已定义了数组的大小,为什么这里赋值就出错了呢?恳请各位高手指点一下!
猜想,AlarmNotificationLoginInfo构造函数中username 为null
回复一:
anli[i].Username = kvp.Value;
改为:
anli[i].Username = kvp.Value==null?"":kvp.Value;
回复二:
AlarmNotificationLoginInfo[] anli = new AlarmNotificationLoginInfo[dic.Count]
anli[0]是null的,应该先new AlarmNotificationLoginInfo()
还有
i++;应该放在foreach内。
回复三:
AlarmNotificationLoginInfo 类是这么写的:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/// <summary>
/// AlarmNotificationLoginInfo 的摘要说明
/// </summary>
public class AlarmNotificationLoginInfo
{
public AlarmNotificationLoginInfo()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
private string username;
private string password;
/// <summary>
/// 用户名
/// </summary>
public string Username
{
get { return username; }
set { username = value; }
}
/// <summary>
/// 密码
/// </summary>
public string Password
{
get { return password; }
set { password = value; }
}
}
lyb018提示说“AlarmNotificationLoginInfo[] anli = new AlarmNotificationLoginInfo[dic.Count]
anli[0]是null的,应该先new AlarmNotificationLoginInfo()”,那是不是要先对对象数组进行初始化?
我又加上了下面这段代码:
AlarmNotificationLoginInfo[] anli = new AlarmNotificationLoginInfo[dic.Count];
int i = 0;
for (i = 0; i < anli.Length; i++)
{
anli[i].Username = "";
anli[i].Password = "";
}
但是调试仍然抛出异常:使用new关键字创建对象实例,是不是要
AlarmNotificationLoginInfo a = new AlarmNotificationLoginInfo();
然后再对对象a进行初始化呀?恳请各位解答。
回复四:
AlarmNotificationLoginInfo[] anli = new AlarmNotificationLoginInfo[dic.Count];
我已经创建了对象实例,为什么还会报空指向的异常呢?
精华推荐:如何让gridview中的checkbox根据数据库情况默认选中?
AlarmNotificationLoginInfo[] anli = new AlarmNotificationLoginInfo[dic.Count];
这里你要新建一个对象:
AlarmNotificationLoginInfo anliTest = new AlarmNotificationLoginInfo();
anliTest.Username = "放入你想要的值";
anliTest.Password = "放入你想要的值";
然后加到数组中.
AlarmNotificationLoginInfo[]这个是一个AlarmNotificationLoginInfo这种类型的数组,所以要先实例化一个这种类型,然后加进去.
AlarmNotificationLoginInfo anliTest = new AlarmNotificationLoginInfo();
switch (kvp.Key)
{
case "username":
anliTest.Username = kvp.Value;
break;
case "password":
anliTest.Password = kvp.Value;
break;
default:
break;
}
anli[i] = anliTest;
是kvp为null吧???
你赋值前先判断下kvp是不是null!
kvp不为null
回复五:
还有一个问题想请教一下:
现在我的代码按网友lyb018的提示修改如下:
if (dic != null && dic.Count > 0)
{
foreach (KeyValuePair<string, string> kvp in dic)//取内容
{
switch (kvp.Key)
{
case "username":
al.Username = (kvp.Value == null ? "" : kvp.Value);
break;
case "password":
al.Password = (kvp.Value == null ? "" : kvp.Value);
break;
default:
break;
}
}
//获取记录条数
for (int i = 0; i < dic.Count; i++)
{
anli[i] = al;
}
}
现在能将对象放在对象数组中了。调试是取到的记录只有一条,但是dic.Count=2(我知道i<dic.Count肯定是不对的),但我现在怎么获得蓝色部分记录的条数,也就是下面标红的循环,怎么获取i<对象个数,这个对象个数怎么获得?恳请高手解答。
也就是如何根据dic.Count获取应该分配多大的空间给数组?
C# code
public AlarmNotificationLoginInfo[] mappingByNotificationLoginInfo(Dictionary<string, string> dic)
{
AlarmNotificationLoginInfo[] anli = new AlarmNotificationLoginInfo[dic.Count];
int i = 0;
try
{
if (dic != null && dic.Count > 0)
{
foreach (KeyValuePair<string, string> kvp in dic)
{
if (i < dic.Count)
{
anli[i] = new AlarmNotificationLoginInfo();
switch (kvp.Key)
{
case "username":
anli[i].Username = kvp.Value;
break;
case "password":
anli[i].Password = kvp.Value;
break;
default:
break;
}
}
i++;
}
}
}
catch (Exception e)
{
throw e;
}
return anli;
}
C# code foreach (KeyValuePair<string, string> kvp in dic)
{
if (i < dic.Count)
{
switch (kvp.Key)
{
case "username":
anli[i].Username = kvp.Value;
break;
case "password":
anli[i].Password = kvp.Value;
break;
default:
break;
}
}
}
i++;
学习是个过程,我们总是先看人家的代码,自己在学习到,以后遇到就会编译啦!感兴趣的话,自己调一下吧!