Is is possible to alternate a property each time it is called?

I have the class below in my project and for a test purpose, each time I access the my_byte property I want its elements to alternate.

namespace my_namespace
{
    public class my_class
    {
        public  byte[] my_byte { get; set; } = { 0x00, 0x00, 0x00 };
    }
}

What I mean is when my_byte is accessed it now returns 0x00, 0x00, 0x00. What I want is next time it is called it will return 0xFF, 0x00, 0x00 and the other time again 0x00, 0x00, 0x00 so forth and so on… So it will alternate.

Is this even possible?

EDIT:

In a main class the method is called as:

    namespace my_namespace
    {
        public class Main_Class
        {
    Class_X obj = new Class_X()
    
        Thread.Sleep(1000);
        byte[] my_received_data= obj.my_method(a, b, c); 
}}

This my_method is in a class:

namespace my_namespace
{
    class Class_X
    {
      public byte[] my_method(byte[] a, int b, bool c)

        {
        
        //here method works properly
}
}

then I have an interface

   namespace my_namespace
    {
  
        
        public interface IMy_Interface
        {
            byte[] another_method(byte[] a, bool b, Class_Z c);
        }
    }

Then here is a class of the interface above:

namespace my_namespace
{
    public class Class_Q:IMy_Interface
    {
        my_class obj_x = new my_class();
        
                public byte[] another_method(byte[] a, bool b, Class_Z c)
        {
       
       //this calles properties from my_class
}

}
}

Here is the class with props in question:

namespace my_namespace
{
    public class my_class
    {
        public  byte[] my_byte { get; set; } = { 0x00, 0x00, 0x00 };
    }
}

As you see before each time the props are retuned, new obj_x created which doesnt let the boolean status to alternate 🙁

EDIT2:

namespace my_namespace
{
    class my_class 
    {
        const int index = 2;
        private bool alternate = true;
   
        public byte[] SendT(byte[] t_sent, bool test_port)
        {
            byte[] returnValue = null;

            if (t_sent[2] == 0xD3)
            {
             

                if (alternate)
                {
                    returnValue = {0x00, 0x00, 0x00  };
                }

                else 
                {
                    returnValue = {0xFF, 0x00, 0x00   };

                }

                alternate = !alternate;
            }

            else
            {
                return null;
            }

            return returnValue;
        }
    }
}

Answer

You would want your 2 byte arrays and a boolean which will flip back and forth each time the property is accessed.

namespace my_namespace
{
    public class AlternatePropertyClass
    {
        public byte[] AlternatingBytes
        { 
            get
            {
                // Get the alternating array value
                //
                return AlternatingByteArray();
            }
        }

        // The byte arrays to alternate between
        //
        private byte[] bytes1 = new byte[] { 0x00, 0x00, 0x00 };
        private byte[] bytes2 = new byte[] { 0xFF, 0x00, 0x00 };

        // The state variable indicating which array to return
        //
        private bool isFirstByteArray = false;

        // The alternating function which changes the state variable back and forth
        //
        private byte[] AlternatingByteArray()
        {
            // Flip the value of the boolean
            //
            isFirstByteArray = !isFirstByteArray;

            // Return the appropriate array based on the state of isFirstByteArray as a ternary operation
            //
            return isFirstByteArray ? bytes1 : bytes2;
        }
    }
}

Test:

var c = new AlternatePropertyClass();
Console.WriteLine(c.AlternatingBytes[0].ToString());
Console.WriteLine(c.AlternatingBytes[0].ToString());
Console.WriteLine(c.AlternatingBytes[0].ToString());
Console.WriteLine(c.AlternatingBytes[0].ToString());

Results:

0
255
0
255

Here is a static version that can be called from wherever.

public static class AlternatingBytes
{
    private static byte[] bytes1 = new byte[] { 0x00, 0x00, 0x00 };
    private static byte[] bytes2 = new byte[] { 0xFF, 0x00, 0x00 };

    private static bool isFirstByteArray = false;

    public static byte[] GetAlternatingBytes()
    {
        isFirstByteArray = !isFirstByteArray;

        return isFirstByteArray ? bytes1 : bytes2;
    }
}

From your code you would call it like:

var bytes = AlternatingBytes.GetAlternatingBytes();

Leave a Reply

Your email address will not be published. Required fields are marked *