首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C# Bloomberg:如何循环数组,创建instrument对象,以及添加到instruments类

C# Bloomberg:如何循环数组,创建instrument对象,以及添加到instruments类
EN

Stack Overflow用户
提问于 2019-02-14 05:53:12
回答 1查看 57关注 0票数 2

我正在使用彭博社的C#网络服务代码来下载投资信息。

我正在努力找出使用字符串数组同时下载多个仪器的正确方法。instruments类的instrument成员是instruments对象的数组。您必须为请求的每个仪器创建一个单独的仪器对象,并将每个对象添加到数组中。然而,我仍然是C#的新手,我正在努力寻找正确的方法将多个instrument对象添加到instruments类中。下面的代码只是返回数组中的最后一次投资,因为循环中的最后一行似乎替换了先前的投资对象。

任何帮助都是非常感谢的。

谢谢。

代码语言:javascript
复制
 string[] investments = { "BBG000BHGCD1", "BBG000BB2PW9" };

             Instruments instruments = new Instruments();

             foreach (string inv in investments)
             {
                 Instrument instr = new Instrument();
                 instr.id = inv;
                 instr.yellowkeySpecified = false;
                 instr.typeSpecified = true;
                 instr.type = InstrumentType.BB_GLOBAL;
                 instruments.instrument = new Instrument[] { instr };
             }


             // Submitting request
             SubmitGetActionsRequest req = new SubmitGetActionsRequest();
             req.headers = getActionHeaders;
             req.instruments = instruments;

             submitGetActionsRequestRequest subGetActReqReq = new 
 submitGetActionsRequestRequest(req);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-02-14 05:58:45

将您的循环更改为:

代码语言:javascript
复制
        Instruments instruments = new Instruments();

        var myList = new List<Instrument>();

        foreach (string inv in investments)
        {
            myList.Add(new Instrument
            {
                id = inv,
                yellowkeySpecified = false,
                typeSpecified = true,
                type = InstrumentType.BB_GLOBAL
            });

        }

        instruments.instrument = myList.ToArray();
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54680023

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档