Home > other >  How can I use the data in private area when I click the button
How can I use the data in private area when I click the button

Time:12-29

I am trying to get bytes from a private class and using it in btn.click.What should I do for doing this?

Here is the sample code:


private void CameraConnnection_DataReceived(object sender, BReceivedData x)
        {
            byte[] data = e.ReceivedData;
        }

        private void BtnGetImage_Click(object sender, EventArgs e)
        {
            unsafe
            {
               byte a[]= e.ReceivedData;// This is not working how can I get received data from there.There is a event and it's triggering from there.
            }

CodePudding user response:

Since EventArgs class doesn't have ReceivedData field or property, the code below doesn't compile:

private void BtnGetImage_Click(object sender, EventArgs e)
{
  ...
  // Compile time error: ReceivedData doesn't exist
  byte a[] = e.ReceivedData;
  

You can obtain and save ReceivedData on DataReceived event but use it on Click:

private byte[] receivedData = new byte[0];

private void CameraConnnection_DataReceived(object sender, BReceivedData x)
{
    // ?? new byte[0] - to be on the safe side and never have null
    receivedData = e.ReceivedData ?? new byte[0];
}

private void BtnGetImage_Click(object sender, EventArgs e) {
  byte[] a = receivedData;

  ...
}
  • Related