26 Haz

C# Indent String With Spaces

This example shows how to indent strings using method for padding in C#. “String.PadLeft” static method is used to repeat spaces.

Usage:

            Console.WriteLine(Indent(0) + "Versions");
            Console.WriteLine(Indent(2) + "Version 1");
            Console.WriteLine(Indent(4) + "Version 1.1");
            Console.WriteLine(Indent(4) + "Version 1.2");
            Console.WriteLine(Indent(6) + "Version 1.2.1");
            Console.WriteLine(Indent(2) + "Version 2");
            Console.WriteLine(Indent(4) + "Version 2.1");
            Console.WriteLine(Indent(4) + "Version 2.2");
        public static string Indent(int count)
        {
            return "".PadLeft(count);
        }

Output is like below.

Versions
  Version 1
    Version 1.1
    Version 1.2
      Version 1.2.1
  Version 2
    Version 2.1
    Version 2.2
25 Haz

C# Update A GUI Element From Another Thread

This examples shows how to update a user interface element from another thread. To do this, we must update to the user interface in user interface thread. Invoke method in Control class implements this task.

Sample Usage:

            //Usage 1
            txtSampleTextBox.Invoke(new Action(() => txtSampleTextBox.Text = "Sample Text"));

            //Usage 2
            txtSampleTextBox.Invoke((MethodInvoker)(() => txtSampleTextBox.Text = "Sample Text"));
25 Haz

Murphy’s Laws About Programming

  1. If a program is useful, it will have to be changed.
  2. If a program is useless, it will have to be documented.
  3. The value of a program is inversely proportional to the weight of its output.
  4. Any given program will expand to fill all the available memory.
  5. Program complexity grows until it exceeds the capability of the programmer who must maintain it.
  6. Any given program, when running, is obsolete.
  7. Any given program costs more and takes longer each time it is run.
  8. murphys law about programming

  9. Every non- trivial program has at least one bug
    • Corollary 1 – A sufficient condition for program triviality is that it have no bugs.
    • Corollary 2 – At least one bug will be observed after the author leaves the organization.
  10. Bugs will appear in one part of a working program when another ‘unrelated’ part is modified.
  11. The subtlest bugs cause the greatest damage and problems.
    • Corollary – A subtle bug will modify storage thereby masquerading as some other problem.
  12. A ‘debugged’ program that crashes will wipe out source files on storage devices when there is the least available backup.
  13. A hardware failure will cause system software to crash, and the customer engineer will blame the programmer.
  14. A system software crash will cause hardware to act strangely and the programmers will blame the customer engineer.
  15. Undetectable errors are infinite in variety, in contrast to detectable errors, which by definition are limited.
  16. Adding manpower to a late software project makes it later.
  17. Make it possible for programmers to write programs in English, and you will find that programmers can not write in English.
24 Haz

C# ReadOnly Wrapper Collection

This examples shows how to create a readonly wrapper collection. If you have a private collection in a class and you only want to access as readonly, you can use AsReadOnly method in “List” generic class in System.Collections.Generic namespace. This method says that “I don’t want people to be able to modify the list content.” It returns a read-only wrapper collection.

Also you can expose your list through a property of type IEnumerable. ( “AsReadOnly” method is mentioned above is prefered.)

Sample Usage:

    public class ReadOnlyCollectionExample
    {
        private List<string> _collection = new List<string>();
        public IList<string> Collection1
        {
            get
            {
                return _collection.AsReadOnly();
            }
        }

        // Adding is not allowed - only iteration
        public IEnumerable<string> Collection2
        {
            get { return _collection; }
        }
    }