【问题标题】:Static method doesn't return value静态方法不返回值
【发布时间】:2016-09-20 03:43:09
【问题描述】:

我做错了什么?一旦执行超出VKRequest.Dispatchtmp 就会丢失数据并返回null

更新

public class GroupClass
{
    private static LinkedList<GroupClass> tmp = new LinkedList<GroupClass>();
    /* some code */
    public static LinkedList<GroupClass> GetGroupList()
    {
        VKRequest.Dispatch<VKList<VKGroup>> ( new VKRequestParameters ( "groups.get", "extended", "1", "filter", "admin, editor, moder", "fields", "photo_100" ), ( res ) =>
        {
            if ( res.ResultCode == VKResultCode.Succeeded && res.Data.count > 0 )
            {
                var item = res.Data.items[0];
                tmp.AddLast( new GroupClass ( item.id, item.name, item.screen_name, item.photo_100 ) );                            
            }
        }); //here
        return tmp;
    }
}

【问题讨论】:

  • 你在哪里初始化 tmp?
  • tmp 应该在方法内部或外部初始化为静态成员。你能更新你的代码吗?

标签: c# windows-phone-8.1 vk-sdk


【解决方案1】:

在调用 lambda 表达式之前,您应该将变量复制到局部变量中。你可以找到更多解释here

public class GroupClass
{
    private static LinkedList<GroupClass> tmp = new LinkedList<GroupClass>();
    /* some code */
    public static LinkedList<GroupClass> GetGroupList()
    {
        var tmp1 = tmp;
        VKRequest.Dispatch<VKList<VKGroup>> ( new VKRequestParameters ( "groups.get", "extended", "1", "filter", "admin, editor, moder", "fields", "photo_100" ), ( res ) =>
        {
            if ( res.ResultCode == VKResultCode.Succeeded && res.Data.count > 0 )
            {
                var item = res.Data.items[0];
                tmp1.AddLast( new GroupClass ( item.id, item.name, item.screen_name, item.photo_100 ) );                            
            }
        }); //here
        return tmp1;
    }
}

【讨论】:

  • 感谢您的帮助,但仍然null
猜你喜欢
  • 2013-06-09
  • 1970-01-01
  • 2018-02-20
  • 1970-01-01
  • 2018-05-23
  • 1970-01-01
  • 1970-01-01
  • 2021-12-31
  • 1970-01-01
相关资源
最近更新 更多