Latest Entries »

This is my first post on C#. Funny why I cannot create “C#” category, have to use “C Sharp” instead. Wells…

Following up on my previous discussion about C++ Mixins, I went on to see if C# was able to do it as well. I then realized that this capability has been sitting in my computer for ages just waiting for today to be discovered by myself!

Incidentally, mixins could be implemented using Interfaces and Extension Methods introduced in C# 3.0 which is the version of VS2008 i am currently using. Furthermore, it seems that the implementation in C# is even more powerful.

Let’s first take a look at mixins in C#.

class Program
{
    static void Main(string[] args)
    {
        // our Son can do all the daily stuff a happy child can do.
        Son son = new Son();
        son.Talk();
        son.Eat();
        son.Walk();
        son.Kick();
        son.Jump();
    }
}

// We demostrate in C# where a class can derive from a parent class
// and also from multiple Interfaces.
// this multi-interface is the fundamentally how mixin is possible.
public class Father { }
public class Son : Father, IMouth, ILeg { }
// by implementing the IMouth and ILeg interfaces,
// our Son gains the respective abilities
// through extension methods below

// The mouth interface and extension method
public interface IMouth {}
public static class Mouth
{
    public static void Talk(this IMouth mouth)
    {
        Console.WriteLine("Talking");
    }

    public static void Eat(this IMouth mouth)
    {
        Console.WriteLine("Eating");
    }
}

// The leg interface and extension method
public interface ILeg {}
public static class Leg
{
    public static void Walk(this ILeg leg)
    {
        Console.WriteLine("Walking");
    }

    public static void Kick(this ILeg leg)
    {
        Console.WriteLine("Kicking");
    }

    public static void Jump(this ILeg leg)
    {
        Console.WriteLine("Jumping");
    }
}

Unlike C++, C# cannot take an unknown class type as base class, hence Extension Methods seems to be the only way we know so far. However, the C# implementation offers some unique benefits that C++ does not have.

In our previous C++ examples in here, notice that our mixin classes have no knowledge of its base class, hence, they have no way to access base methods and variables. Basically, the Mouth and Leg in C++ always act on their own without control from the Son.

In C#, things get a little bit better. Due to the fact that our Son implements the IMouth and ILeg interfaces, the Extension Method can interface with the Son. Take for example the following code.

class Program
{
    static void Main(string[] args)
    {
        Son son = new Son();
        son.Eat(); // Chewing
        son.Talk(); // I Like Minced Beef
    }
}

public class Father { }
public class Son : Father, IMouth
{
    // Son implements the IMouth interface to give some access to the Mouth mixin
    private String mFood;
    public Son() { mFood = "Beef"; }
    public String food { get { return mFood; } set { mFood = value; } }
}

// the IMouth interface enforces the requirement for the food property
public interface IMouth
{
    String food { get; set; }
}
public static class Mouth
{
    // the food property is used and the value is provided
    // by the implementing class
    public static void Eat(this IMouth mouth)
    {
        Console.WriteLine("Chewing");
        mouth.food = String.Concat("Minced ", mouth.food);
    }

    public static void Talk(this IMouth mouth)
    {
        Console.WriteLine("I Like " + mouth.food);
    }
}

Now, not only our Son gets new abilities, our Son can also excercise control over its new abilities. “With great powers comes great responsibilities”!

C++ Mixins, A Powerful Concept

I just came across a very interesting concept, and imo, quite powerful. Unfortunately, I currently only knows that it works on C++ using my VS2008. I find it even more amazing that this concept stems from ice cream toppings, that is according to wikipedia and some other coherent sources.

One customer enters the ice cream palor, selects a chocolate ice cream as the base, and then adds oreo toppings to create his own oreo-chocolate ice cream. Another customer comes along and chooses the same chocolate ice cream base, but this time adds cherry and chips to create his cheery-chips-chocolate ice cream. This concept was first coined as Mix-ins.

Similarly, in programming, the objective is to allow the programmer to select a base class and then add functional features to it, the way toppings are added to ice cream. Quite amazingly, the resultant code looks exactly as described in words: base ice cream wrapped around with toppings. We’ll see how this is possible.

Suppose today we want to create a Man with a Mouth. Traditionally, OOP makes you create a 2 classes: a Man and a Mouth, and then put the Mouth in the Man.

public class Mouth {
public:
        void Talk() { Console::WriteLine("Hello World"); }
};

public class Man {
public:
       Mouth mouth; // OOP: the Man has a Mouth
};

int main()
{
        Man peter;
        peter.Talk(); // real traditional OOP.
        return 0;
}

All neat and nice. And we’ve got a reusable Mouth that we can use in a Woman class for example. We also have a reusable Man class, but if and only if the new Man also has got a Mouth.

Suppose now I want to create a Man that also has the functionality of Legs, we cannot really create Legs and add them inside Man directly, unless we alter the source which breaks the reusability. The traditional solution is to create a WalkingMan that inherits Man. So there you have it:

public class WalkingMan : Man
{
public:
        void Walk() { Console::WriteLine("I'm walking on sunshine"); };
}

int main()
{
        WalkingMan peter;
        peter.Talk(); // real traditional OOP.
        peter.Walk(); // now my man can walk too!
        return 0;
}

Although the above solution works, it poses a critical problem: every time we want a Man with new abilities, we have to write a new Man class with that ability. And the vicious cycle continues because the new class may not have other abilities that another programer wants. For example, you cannot create a SkippingWalkingMan from a WalkingMan and a SkippingMan. The former is true even if multiple inheritance is possible.

Enter mixins, or in our local ice cream lingo, toppings. To turn our class library into an ice cream parlor, we first need to create a some base flavors:

public class Chocolate {

};
public class Vallina {

};

Now we create the mixins or toppings. This is where is cleverness comes in:

template<class T>
public class Oreo : public T {
public:
        void LickOreo() { Console::WriteLine("Orea is great"); }
};
template<class T>
public class Chips : public T {
public:
        void LickChips() { Console::WriteLine("Chips is crunchy"); }
};
template<class T>
public class Cherry : public T {
public:
        void LickCherry() { Console::WriteLine("Cheery is Juicy"); }
};

finally in our program, we can create any ice cream we want by literally wrapping our ice cream with mixins (or toppings)!

int main()
{
        Oreo<Chocolate> OreaChocolateIceCream;
        Cherry<Chips<Chocolate>> CherryChipsChocolateIceCream;
        Oreo<Chips<Cherry<Vallina>>> OreoChipsCherryVallinaIceCream;
        // all so yummy!
        return 0;
}

The templated class is called a Mixin class, and they provide a powerful way to create modular functionality that can be reused over and over again. As a creator of a Mixin library, you can create new ability classes without worrying about the base, thus allowing very high code portability.

There’s one caveat though. If 2 Mixins have functions of the same name, the outter most Mixin will override. If all the Mixins had Lick(), you would be Lick()ing the outter most Mixin! But this can be avoided by prefixing function names with the class name. Simple!

Windows Host File

There is a forgotten file in Windows (Vista) known as a host file and resides in the depths of C Drive at c:\windows\system32\drivers\etc.

The host file has got no file extension but its an ascii file so you can open it in notepad and start editing it because it allows you to rewrite the IPs of hosts that will otherwise resolve correctly by your network connection’s DNS.

On MAC, apparently it’s harder to get there but you can try this (not tested):

Enter the Terminal (Applications / Utility). Type this:
sudo /Applications/TextEdit.app/Contents/MacOS/TextEdit /etc/hosts

With a little bit of imagination, this bit of information will become very useful. As to what use, I will not suggest here.

Acer built-in recovery

Acer laptops (I believe all) comes loaded with a hidden recovery partition in the hard disk. To boot from this partition, simply press ALT+F10 (at least for the Aspire series) at the “boot up” screen where it says press F2 to enter settings.

So if the next time you go for a repair and all they are going to do is a “format”, “reinstall”, or “recovery”, then you can also do it yourself. Save that $80 (or whatever the amount).

人啊!

人啊!

沒錢的時候,養豬;
有錢的時候,養狗。

沒錢的時候,在家裡吃野菜;
有錢的時候,在酒店吃野菜。

沒錢的時候,在馬路上騎自行車;
有錢的時候,在客廳裡騎自行車。

沒錢的時候想結婚;
有錢的時候想離婚。

沒錢的時候老婆兼秘書;
有錢的時候秘書兼老婆。

沒錢的時候假裝有錢;
有錢的時候假裝沒錢。

人啊,都不講實話:

說股票是毒品,都在玩;
說金錢是罪惡,都在撈;

說美女是禍水,都想要;
說高處不勝寒,都在爬;

說煙酒傷身體,就不戒;
說天堂最美好,都不去!!!

當今社會,窮吃肉,富吃蝦,領導幹部吃王八;

男想高,女想瘦,狗穿衣裳人露肉;

過去把第一次留給丈夫;
現在把第一胎留給丈夫。

鄉下早晨雞叫人,
城裡晚上人叫雞;

舊社會戲子賣藝不賣身,
新社會演員賣身不賣藝。

人生是什麼?
只 用 了 4 4 個 字 , 就 把 人 生 講 完 了 ….
所 以 人 與 人 , 有 啥 好 計 較 的 咧 ?
快樂好相處比較重要啦!

1 歲 時 出場亮相
10 歲 時 功課至上
20 歲 時 春心盪漾
30 歲 時 職場對抗
40 歲 時 身材發胖
50 歲 時 打打麻將
60 歲 時 老當益壯
70 歲 時 常常健忘
80 歲 時 搖搖晃晃
90 歲 時 迷失方向
100 歲 時 掛在牆上

祝大家愉快,好好做人!

online inventory count for over ordering

The only way to prevent over-ordering is to lock all stocks at the moment they are added to cart. This is analogous to the real world when in a supermarket where the groceries are “deducted” from the shelves when they are “added” to the real shopping cart.

However, when customers in the supermarket decides not to check out and buy, they do not put the groceries back to the shelves. This is also true online and it becomes a problem because the stock count cannot be added back to cart in a timely fashion and subsequent buyers will have difficulty selecting the items they want.

In the online world, this implementation is only compulsory for time-sensitive products such as ticket bookings and the current way to overcome the problem is to set a time-out value where the items added to cart will be replenished back to the shelf stock count.

Lesson #1. Never judge a file by its icon. I was mislead to believe it was an installation exe because of the icon image. I should have been more careful.

First day of chinese new year and my itchy fingers got myself into some really serious trouble: I saw a mysterious “installation” exe in my downloads folder and, as I always did, wanted to double click it to see what installation is that that I downloaded (I download installations files every now and then).

Lession #2. Turn off all network connections immediately when you get infected to prevent further damage and quarantine your computer, unless you know it’s safe not to do do.

I realised my mistake immediately after the double click. And a moment later, Windows Defender poped up telling me that it has detected Adware:win32/rugo. Luckily for me, I consider Adwares as the least harmful among the many other types of irritants. Knowing that it is an Adware and not some trojan or keylogger that takes control over your system, I can safely keep the Internet connection on without having to worry of spreading the virus to other computers in the network or getting my computer compromised by some hacker.

Lesson #4. Once you think you are infected, do not simply turn off or restart your computer.

Complete infection may not start immediately, and sometimes requires further action before the full power will be unleashed. Hence, the next best step is actually not to do anything on the computer. Not even restarting the computer. A restart can usually be the worse thing to do as it allows the infection to enjoy a clean startup procedure. If you have access to another computer, use that to proceed. Otherwise, with the infected computer, the next thing to do is…

Lesson #3. Find out that the malware does.

More often than not, you probably won’t find any direct recipe for the antidote. This is because the malware changes its appearance over different released versions. However, the behavior and the method of infection generally does not change. Hence, the first thing to look out for when researching for the malware is to understand how it operates.

Lesson #4. Look out for location and names of process, files and registry.

On XP and Vista systems: Many infection start as such.

  1. You run a poisonous program file directly by clicking on it, or indirectly by allowing some other program to start it.
  2. The program file creates poison files, registry keys, configurations and permissions and injects them to various locations in the computer.
  3. Amongst created poison files, a watchdog program is created and started. When this watchdog starts, something known as a “process” is created. This process will have a permission configuration such that there is no way to cancel/stop it during normal operation. Deleting the watchdog program will not remove the process. By now, you are infected.
  4. The watchdog process runs every now and then to ensure that the poison files are not deleted, including the watchdog program. If the converse is ture, the process will recreate them. Recreated files may have completely different locations and filenames, making you think you had already deleted them.
  5. Sometimes, the process is programmed to complete some final stages of the infection work on shutdown/restart . Hence give it the chance to do so.
  6. Since the watchdog program can never be deleted normally, every time the computer enter Windows, the process will be started by the watchdog program and the cycle repeats.

Hence the the period where the infection takes place is really the time you enter Windows until the time you shutdown completely. If there is any chance for you, it is the ability to interrupt the watchdog process. However, for very powerful virus, they can infect you even before you enter windows, before you have any chance to interrupt the boot sequence. Once you can stop the process, poison files will not be recreated and the infection can be removed successfully.

The CSS :hover event is only supported for the anchor element <a> and hence to simulate the correct :hover behavior for other elements such as td:hover, we need a javascript hack.

The idea is to change the class of the hoverable element so that the CSS can be applied to the class i.e. td.hoverable.

To do this, we attach a function to the onmouseover and onmouseout events of the element to add an remove the class name respectively.

free Dueter styled bags to give away

My father works in a bag manufacturing company and brings home completed bag samples everytime he comes back from Mynmar. I have so many bags tucked everywhere and I can’t bear to throw them away. If anyone or anyone knows anyone wants free bags, please tell me.

walking 5.5 km/h

The last time i walked from bedok to commonwealth, it took me 6 hours from 8am to 2pm, but i was’t able to calculate how fast i was walking – continuously.

I just got back from a 5 hour walking trip from harbourfront to bedok. Super aching now! This time, however, i think i can calculate a reasonable walking speed because i took a cab back to commonwealth!

While I used the slowest means to get there, I certainly took the fastest cab ever (at least for myself). It was cruising at an average of 115 km/h and at one stretch along PIE it hit 130 km/h!! The whole cab was shaking madly as the driver swirves (.. pardon the spelling) its way through. The whole time I was holding on to the seat belt, ensuring it is locked tight. Reached in less than 15 mins. the last horror of the day was the fare: $20.40, but he said, “Aiyah, 40 cents no need lah”.

With these data, I would be walking at average of 5.5 km/h.

Powered by WordPress | Theme: Motion by 85ideas.