none
Convert private static readonly sbyte[] to byte[] RRS feed

  • Question

  • Dear All,

    I am stuck on it and normally it is ok under the code

    private static readonly sbyte[] dataArr;
    byte[] bArr = (byte[])(Array)dataArr;  // OK
    
    

    But can't type cast in shown below or do i need to re implement again?

    private static readonly sbyte[] dataArr;
    byte[] bArr; 
    
    for (int i = 0;i <Length; i++)
    {			
       bArr[i] = (byte[])dataArr[i];
    }

    Here is error message Cannot convert type 'sbyte' to 'System.Array'

    I hope someone will advise to get the right solution.

    Thanks and best regards

    Thursday, February 21, 2013 3:43 AM

Answers

  • The code is trying to convert a byte into an array of byte. It should just be casting to byte as below:

    for (int i = 0;i <dataArr.Length; i++)
    {			
        bArr[i] = (byte)dataArr[i];
    }

    • Marked as answer by Mr Ko Ko Thursday, February 21, 2013 6:49 AM
    Thursday, February 21, 2013 4:04 AM
  • Hi,

    True and I forgot to type cast busy with other things.

    I have solved the issue.

    Best regards

    • Marked as answer by Mr Ko Ko Thursday, February 21, 2013 6:48 AM
    Thursday, February 21, 2013 6:48 AM

All replies

  • The code is trying to convert a byte into an array of byte. It should just be casting to byte as below:

    for (int i = 0;i <dataArr.Length; i++)
    {			
        bArr[i] = (byte)dataArr[i];
    }

    • Marked as answer by Mr Ko Ko Thursday, February 21, 2013 6:49 AM
    Thursday, February 21, 2013 4:04 AM
  • Hi,

    True and I forgot to type cast busy with other things.

    I have solved the issue.

    Best regards

    • Marked as answer by Mr Ko Ko Thursday, February 21, 2013 6:48 AM
    Thursday, February 21, 2013 6:48 AM