C#Bloomberg:如何遍历数组,创建工具对象以及将其添加到工具类

时间:2019-02-13 21:53:12

标签: c# bloomberg

我正在使用Bloomberg的C#Web服务代码下载投资信息。

我正在努力找出使用字符串数组同时下载多个乐器的正确方法。 Instruments类的Instrument成员是Instrument对象的数组。您必须为所请求的每个乐器创建一个单独的乐器对象,并将每个对象添加到数组中。但是,我对C#还是很陌生,我一直在努力寻找将多个仪器对象添加到Instruments类的正确方法。下面的代码仅返回数组中的最后一笔投资,因为循环的最后一行似乎替换了之前的投资对象。

感谢您的帮助。

谢谢。

 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);

1 个答案:

答案 0 :(得分:1)

将循环更改为此:

        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();