2007/11/21

Brainstorming: Prefix and Postfix Operators

Another interested code segment talks about prefix and postfix operators:
class IncrementExample
{
 public static void Main()
 {
   int x = 1;

   Console.WriteLine("{0}, {1}", x++, x++);
   Console.WriteLine("{0}, {1}", ++x, ++x);
 }
}
The result is
1, 2
4, 5

Since x++ has a postfix operator, that means x will give its value to Console.WriteLine first, then perform the ++ operation. In contrast, prefix operator will perform ++ operation first, then give the value to Console.WriteLine.

Brainstorming: An interested interview question

I saw this code segment on a blog:
public class A
{
  void PrintA()
  {
    Console.WriteLine("Print A");
  }

  class B : Program
  {
    new void PrintA()
    {
      Console.WriteLine("Print B");
    }
  }

  public static void Main()
  {
    A Atemp = new A();
    B Btemp = new B();

    Atemp.PrintA();
    Btemp.PrintA();
  }
}
What do you think the result will be? Don't forget this is a Interview question, that means you have to answer no more than few minutes.

The answer is:
Print A
Print A
Why not and how to get the following result?
Print A
Print B

The key point is the inner/nested class. We have to declare it is a public class so it's visible to everyone.

2007/11/20

Photo and Archive Merger (link updated)


Name: Kenny's Photo and Archive Merger
Version: 1.0
OS: Windows 2000/XP with .Net Framework 2.0 installed
Introduce:
In some circumstances you will need to hide your archive. For example, some free space only allow you to upload and store photos/images, and this program can help you to hide your archive in a photo/image. I believe you can have more ways to use this tool depend on your imaginations... :)
How to use:
1.Select a photo/image (to be a cloak)
2.Select an archive (to be cloaked)
3.Select a destination (don't forget to append the extension name, e.g. ".jpg")
4.Click Merge icon
That's it!
How to split:
Either 1.change the extension name from image type (.jpg) to original archive type (.rar or .zip) or 2.use the corresponding archiver (WinRAR or WinZIP) to open the photo/image directly.

2007/11/07

Closing a windows form

How to close a windows form? Many people will create a click event and call the Form class' Close() method like this:
private void btnExit_Click(object sender, EventArgs e)
{
  this.Close(); //or this.Dispose();
}

Since you created the Click event just for closing the form, why don't we try other simpler approach? We can set the button's DialogResult property to a non-default value (any value other than None), so that .Net framework will close the form for us automatically when user click that button. This way we can handle button clicks without an event handler. Actually the DialogResult property will return a value depends on how the form is closed, and we can use that value to do many things.

What if we also have an event handler for the same button's Click event? Well, the Click event will still be executed, and it will be done first. After the event finishes and returns control to framework, the form will be closed.

By the way, this cannot replace the Application.Exit(). Don't waste your time on using the button's DialogResult property on the main form's Exit button, it won't work.

If you want to know more details about what's underlying, I'll suggest you to read this book: .NET Windows Forms in a Nutshell.