如何在循环中使用集合

如何在循环中使用集合

本文介绍了如何在循环中使用集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,在Update方法中我有int count,比如说count == 4,然后我用for循环来获取id,version和set。在这种情况下,Id,版本和设置值只获取最后一个值,但如何获取所有值



Hi all, In Update method I had int count, let say count == 4, then i am using for loop to get id, version and set. In this case Id,version and set values getting only the last value, but how to get the all the values

public void Updates(AUnit _aUnit, int Id)
    {
        ImageDetails _details = new ImageDetails(_aUnit, Id);

        int count = (int) _aUnit.ReadBits(8);
        for (int i = 0; i < (int) count; i++)
        {
            _details.ID = (int) _aUnit.ReadBits(8);
            _details.Version = (int) _aUnit.ReadBits(8);
            _details.set = (int) _aUnit.ReadBits(24);
        }

        _details.Rset = _aUnit.Buffer.Skip(10).Take(_details.set).ToArray();

        //MemoryStream ms = new MemoryStream(_details.PortrateImages.First());
        //Image image = Image.FromStream(ms);
        //Bitmap bmp = new Bitmap(image);

        _details.UpdateTime = DateTime.Now.ToString("h:mm:ss tt");
        newData.Add(_details);
    }
public class ImageDetails
  {
    public ImageDetails(AUnit _au, int carouselId)
    {
        carId = carouselId;
        _AUnit = _au;

        _updateTime = "";
    }
    private string _updateTime;
    public int ID { get; set; }
    public int Version { get; set; }
    public int set { get; set; }
    public int carId { get; set; }
    public byte[] Rset { get; set; }
    public AUnit _AUnit { get; set; }
    public byte[] bytes { get; set; }
    public List<byte[]> dataArray = new List<byte[]>();

    public string UpdateTime
    {
        get { return _updateTime; }
        set { _updateTime = value; }
    }

    public List<byte[]> PImages
    {
        get
        {
            List<byte[]> Plogos = new List<byte[]>();
            if (carId == 2)
            {
                Plogos.Add(Rset);
            }
            return Plogos;
        }

    }
    public List<byte[]> LImages
    {
        get
        {
            List<byte[]> Llogos = new List<byte[]>();
            if (carId == 1)
            {
                Llogos.Add(Rset);
            }

            return Llogos;
        }

    }
}





我尝试过:



我试过但我觉得它错了而且没有工作,为id,version和set创建了一个单独的列表,例如:_details.imageList.Add( logoHeader.LogoID);



What I have tried:

I tried but i feel its wrong and not working, Created a seperate list for id, version and set, eg: _details.imageList.Add(logoHeader.LogoID);

推荐答案

#
public void Updates(AUnit _aUnit, int Id)
    {

        int count = (int) _aUnit.ReadBits(8);
        for (int i = 0; i < (int) count; i++)
        {
            ImageDetails _details = new ImageDetails(_aUnit, Id);
            _details.ID = (int) _aUnit.ReadBits(8);
            _details.Version = (int) _aUnit.ReadBits(8);
            _details.set = (int) _aUnit.ReadBits(24);
            newData.Add(_details);
        }

        _details.Rset = _aUnit.Buffer.Skip(10).Take(_details.set).ToArray();

        //MemoryStream ms = new MemoryStream(_details.PortrateImages.First());
        //Image image = Image.FromStream(ms);
        //Bitmap bmp = new Bitmap(image);

        _details.UpdateTime = DateTime.Now.ToString("h:mm:ss tt");
    }





现在的区别是你在Loop中为你的集合创建一个条目并添加每个创建的条目你的收藏(也在循环内)



the difference now is that you create an entry for your collection inside the Loop and add each of those created entries to your collection (also inside the Loop)


这篇关于如何在循环中使用集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 01:10