Click here to Skip to main content
15,914,070 members

Comments by Member 14097984 (Top 7 by date)

Member 14097984 27-Dec-18 13:38pm View    
Perfect. Thank you very much for all your help and support, it has been greatly appreciated, and I have learnt one or two things.

Thanks
Stephen
Member 14097984 24-Dec-18 9:45am View    
Thanks Richard. Please forgive me for coming back one more time, I am still stuck :(

I followed your advice and Created just one class, and in this class I used your code, and it works and correctly copies bytes regardless of the size of array, however, I have 2 man issues that I still have no idea how to solve.

1. How can I set it so that I return 8x byes per block. So if for example I pass in 10 bytes, I would have 1x full 8 byte block, then I would create the next 8 byte block with the last 2 bytes and fill the remaining 6 as 0x00.

2. I can't figure out how this class can return individual objects for my <list> on the main form, so that I can display them on my Data Grid.

Would I need to create a <list> of objects of my Block class within the actual Block class and return these?

I have added my code below which is now all in one class.

Again, I really appreciate if you are able to assist :)

<pre>using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataTest
{
    class ByteBlock
    {
        // 8 Bytes and Offset for Byte Block
        private byte _00 = 0x00;
        private byte _01 = 0x00;
        private byte _02 = 0x00;
        private byte _03 = 0x00;
        private byte _04 = 0x00;
        private byte _05 = 0x00;
        private byte _06 = 0x00;
        private byte _07 = 0x00;
        private int _offset;

        private List<ByteBlock> list = new List<ByteBlock>(); // List of ByteBlocks to return for List/Data Grid




        public ByteBlock(byte[] data, int offset)
        {
            // Loop throught the provided data and build List of ByteBlocks with 8x Bytes and calculated offset's
            // Set each byte value and the offset, then add the object to the List

            byte[] _byteArray = data;
            int _numberOfBytes = data.Count();
            int _blockSize = 8;
            int _offset = offset;

            byte[] tmpDat = new byte[_numberOfBytes];


            do
            {

                Buffer.BlockCopy(_byteArray, _offset, tmpDat, _offset, _blockSize); // copy a block

                _offset += _blockSize;                                         // increment the offset
                _numberOfBytes -= _blockSize;                                  // decrement the number remaining

                if (_numberOfBytes < _blockSize)    // if remainder is less than the blocksize
                    _blockSize = _numberOfBytes;  // set the blocksize to remainder for last block
            } while (_numberOfBytes > 0);



        }






        


        // Public Interface methods for data grid
        public int Offset
        {
            get { return _offset; }
        }

        public byte i00
        {
            get { return _00; }
        }

        public byte i01
        {
            get { return _01; }
        }

        public byte i02
        {
            get { return _02; }
        }

        public byte i03
        {
            get { return _03; }
        }

        public byte i04
        {
            get { return _04; }
        }

        public byte i05
        {
            get { return _05; }
        }

        public byte i06
        {
            get { return _06; }
        }

        public byte i07
        {
            get { return _07; }
        }


    }
}


In my main class I need to some how pass in a byte[] and get back individual (List/Array?) objects of 8x bytes, so that each 8 Byte block becomes a row on my Data Grid.

Again, thank you so much of you can assist.
Regards
Stephen
Member 14097984 24-Dec-18 8:20am View    
Thank you for your help, and merry Christmas.
Thanks
Stephen
Member 14097984 24-Dec-18 6:40am View    
Sorry I don't think I explained myself correctly. The data grid I am using is from DevExpress, not Microsoft, and it requires a <list> object, so what I would like to know is should I create a custom class for the data, and one that performs the 8 byte operations, or should I simply put it all in the one class, and use this class as the data source as well. Lastly, if I need a <list> of objects for my Data Grid, then I can't see how my class could return multiple 8 byte objects/rows, and this was the reason I had created a third class which was a Byte Block Array, that performed these operations and returned individual 8 byte block items, however, that is when it got complecated:)

Thanks again
Stephen
Member 14097984 23-Dec-18 17:16pm View    
Also one last thing, if I go with your suggestion, how would I get the class to return blocks of 8 bytes for my data grid. For example, if I have the one class that I pass in a data[16], and this is a <list> bound to my data grid, how would I get this class to return the 2x blocks of 8 bytes for the grid? Hope that makes sense, sorry, still learning :) thanks again, Stephen.