初始化导致错误的列表时 [英] when initializing a list causing errors

查看:86
本文介绍了初始化导致错误的列表时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Dictionary<string,string> newList = new Dictionary<string,string>();
newList.add("cat", "dog");



从上面的代码中,我试图初始化字典列表.因为在我的情况下,我将其传递给一个函数,该函数被填充并返回.

有时它又回来了却没有被填满.因此,当我调用其显示的对象引用未设置为对象的实例"时.

当我执行View.ReasonErrorCodeList = Model.ReasonErrorCodeList时,如下所示

所以我想在将列表传递给函数之前初始化列表.为此,我向列表中添加了两个值仍然不起作用.

您能给我正确的初始化代码吗?

在presenter类中,我正在调用列表,如下所示,这就是导致问题的原因.
View.ErrorCodeList = Model.ErrorCodeList;



From the above code I''m trying to initialize the dictionary list. Because in my case I pass it to a function where it get filled and comes back.

Some times it comes back without getting filled. so when I invoke its showing "Object reference not set to an instance of an object."

when I do View.ReasonErrorCodeList = Model.ReasonErrorCodeList as you can see below

So I want to initialize the list before i pass it to the function. To do that I added two values to the list still didn''t work.

Could you please give me the right code for initialization.

in the presenter class I''m invoking the list as you can see below and thats when its causing problem.

View.ErrorCodeList = Model.ErrorCodeList;

推荐答案

您的代码不完整,因此很难找出可能出问题的地方.
如果您只需要在访问字典之前检查null (使用简单的if条件),就可以了.
Your code is not complete so its difficult to figure out what could be going wrong.
If you can just check for null (using a simple if condition) before accessing the dictionary, you should be fine.


Dictionary<string,> newList = new Dictionary<string,>();
newList.add("cat", "dog");



是定义字典的正确方法,因为您没有发布完整的代码,因此很难找出问题所在.

尝试发布您的完整代码!

快乐2帮助您



is the right way to define dictionary, because you are not posting your complete code so it''s very difficult to figure out your problem.

Try to post your complete code !

Happy 2 Help You


这篇关于初始化导致错误的列表时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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