pnatk
pnatk

Reputation: 121

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;
        }
    }
}

Upvotes: 0

Views: 253

Answers (2)

hijinxbassist
hijinxbassist

Reputation: 4631

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();

Upvotes: 1

Snail Cadet
Snail Cadet

Reputation: 311

Very possible. You can write your own get method and use a field (or another property) to control the alternation. Here's an example:

namespace my_namespace
{
    public class my_class
   {
      private bool alternate = false;
      public byte[] My_byte
      {
         get
         {
            byte[] returnValue;
            if (alternate)
            {
               returnValue = new byte[] { 0x00, 0x00, 0x00 };
            }
            else
            {
               returnValue = new byte[] { 0xFF, 0x00, 0x00 };
            }
            alternate = !alternate;

            return returnValue;
         }
      }
   }
}

I also recommend you spend some time with the MS Docs. The page on Properties maybe be useful.

Edit - Here's a main that repeatedly gets and prints the values:

      public static void Main()
      {
         var x = new my_class();
         for (int i = 0; i < 10; i++)
         {
            foreach (var y in x.My_byte) {
               Console.WriteLine(y);
            }
            Console.WriteLine("=====");
            Thread.Sleep(1000);
         }
      }

Sample output:

255 0 0 ===== 0 0 0 ===== 255 0 0 ===== 0 0 0 =====

Upvotes: 0

Related Questions