You must Sign In to post a response.
  • Category: .NET

    How to use byte array in another method within same class?

    I know its silly question but I have trouble in accessing byte array in another method other than where it is initialized and also i want to access it in method of another class.

    Thanks ,
    Here is a method where byte array "data" declared and i want to use it in different method say
    "GetData" method.

    Class1.cs

    public void _serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
    int dataLength = _serialPort.BytesToRead;
    byte[] data = new byte[dataLength];
    int nbrDataRead = _serialPort.Read(data, 0, dataLength);
    if (nbrDataRead == 0)
    }

    public void GetData()
    {
    }


    Class2.cs

    public void GetData()
    {}
  • #762490
    Hi Priyanka.

    You can access it by declaring byte array as public or protected in your class1. And inherit the class2 from class1 like below it will work.

    namespace sample
    {
    public class class1
    {
    protected byte[] data = new byte[5];
    public void _serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
    data.ToString();
    }
    public void getdata()
    {
    data.ToString();
    }
    }

    public class class2 : class1
    {
    public void _serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
    data.ToString();
    }
    public void getdata()
    {
    data.ToString();
    }
    }
    }

    Sridhar Thota.
    Editor: DNS Forum.

  • #762495
    but data object getting info from "datalenght"
    as byte [] data = new byte [datalenght] ,
    and datalengh is specified in DataReceived method so how can i get actual data object. . I just want to get access of that objects which declared in dataReceived event.

  • #762555
    Hi

    Declare Protected int dataLength; outside like below.

    public class class1
    {
    protected int dataLength;
    protected byte[] data;
    public void _serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
    //
    }
    public void getdata()
    {
    //
    }
    }

    Regards

    Sai Krishna Reddy.


Sign In to post your comments