BitArray Class with Console.SetCursorPosition(x,y)

CodeKeep C# Feed Maggio 15th, 2008

Description: It is a displaying of BitArray data in specific postion at console windows.
In addition, the method of C#’s Console.SetCursorPosition(x,y) is equivalent to C++ gotoxy(x,y).

Link: http://www.codekeep.net/snippets/a9144fd8-2f2d-4916-96da-6790e1003c14.aspx

static void Main(string[] args)
        {
            //fixed size for BitArray
            int bitSize = 4;

            BitArray bits = new BitArray(bitSize);
            bits[0] = true;
            bits[1] = true;
            bits[2] = false;
            bits[3] = false;
            //Display values for bits
            PrintValues(bits, 1, "bit");

            BitArray moreBits = new BitArray(bitSize);
            moreBits[0] = true;
            moreBits[1] = false;
            moreBits[2] = true;
            moreBits[3] = false;
            //Display values for moreBits
            PrintValues(moreBits, 10, "moreBit");

            //Compare two BitArray with xor method
            BitArray xorBits = bits.Xor(moreBits);

            //Display the result of xor
            PrintValues(bits, 25, "Result xor");

            Console.Read();
        }

        public static void PrintValues(IEnumerable myList, int myWidth, string bitName)
        {          

            //Starting position
            int i = 0;
            int w = myWidth;

            Console.SetCursorPosition(w, i);
            //Display header
            Console.WriteLine("\"{0}\" \n", bitName);

            //increment 1 to place in the next line
            i++;

            foreach (object obj in myList)
            {
                //Cursor position after the header
                Console.SetCursorPosition(w, i);
                //Display data
                Console.Write("{0}", obj);
                //increment i to move in the next line
                i++;
            }
        }

  • .NET
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Comments are closed.

Trackback URI |