C#Bloomberg:如何遍历数组,创建乐器对象,以及添加到乐器类

问题描述 投票:2回答:1

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

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

任何帮助表示赞赏。

谢谢。

 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);
c# bloomberg
1个回答
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();
© www.soinside.com 2019 - 2024. All rights reserved.