Better comparison with enumeration
Suppose you have an enum list and a variable which value comes from external source. What do you usually do to compare against the enum list?
If (MyValue == "3") { //3 means ok, do some stuff }
What if one day you decide that you don't like 3 (for whatever reason) and change it to 30? You will have to find and replace the repeated value all over your project. Yes, you have the FindAndReplace function with most IDE, but holy cow, you only realize that the last couple of 3 meant something else!
So a safer way is to cast the value into appropriate enum for comparison or any other operation.
'VB.NET
dim myEnumValue as TheEnum = CType([Enum].Parse(GetType(TheEnum), _
tempValue.ToString()), TheEnum)
If myEnumValue = TheEnum.NumberThree Then
'..
End If
//C#
TheEnum myEnumValue = (TheEnum)Enum.Parse(typeof(TheEnum), tempValue);
if (myEnumValue == TheEnum.NumberThree) { //.. }
"Hey, that looks useful. I want it in my SuperCoolReusable library" you might say. With the power of Generic, we can put it into a common call.
'VB.NET
Public Shared Function ParseEnum(Of T)(ByVal Value As Object) As T
Return CType([Enum].Parse(GetType(T), Value.ToString()), T)
End Function
//C#
public static ReturnType ConvertStringToEnum<ReturnType>(string Value)
{
return (ReturnType)Enum.Parse(typeof(ReturnType), Value);
}
Another simple yet useful tip!
Have you made your environment pledge?
Facts, in case you wonder :
1. It takes less than half a second to turn off the water tap while brushing your teeth, and that in turn saves 14,600 litres of water a year!
2. It requires less than 0.1% of your brain power to remember to turn lights out when not required, and that action saves 68kilogram of CO2 a year!
3. It requires less than couple of mouse clicks to print 2 pages a sheet, and that setting saves half a tree every year!
Those are my pledges to prevent the earth from deteriorating. You don't have to be involved in lots of activities to save the earth when you can even start small at your own pace. Make your pledge today here! Together, we can
Perth, an option
I went to Perth on 21 Feb 2008 to make my first entry as a PR of OZ. Initially, i planned for Gold Coast, but since the airfare cost as much and i've never been there, i changed my destination. After this trip, i can conclude that my friends have been feeding me wrong information about the place. Yeah, you can say it is boring, but i like the pace and setting. Near to 30, you don't expect me to jump up and down every minute anymore, right? I spent about 3+ days there at an auntie place. She is very friendly, and so are the other family members. Thanks to them, i had a superb yet real relaxing break there. Don't wanna spend my time writing long stories where people don't finish, i will just summarize my itinerary.
Here goes:
20th Feb - took the night train down to Singapore. My first and last. There was place to sleep, but the train ride was noisy and rocky. So if you are a light sleeper, forget it. Next time i rather travel by bus earlier.
21st Feb - loiter around Singapore since my flight was at 3pm. Nothing much to buy after considering currency conversion. Reached Perth at 11pm and had no problem going through the custom. Slept like a pig after supper and a long day
22nd Feb - woke up late and auntie accompanied me down to city just to show me a few tips. After that, i was all alone. Took a bus up to King's Park first and the view was great. Not going to describe here as i sucks at it, so browse the gallery. Walked around the city and explored their factory outlets which are just nearby. Followed auntie's direction home. Nearly lost but i reached in one piece
23rd Feb - Woke up very early to follow auntie to wholesale market. You never get price so cheap here and i was pretty shocked! Where do you get a box (a big one) of brocollis for mere AUD5?! And lots of fruits too! Joined the gang to Fremantle Market since most guides talk about it. Quite disappointed at the size but content matters. We walked around the beach area after and went home to runaway from the burning sun.
24th Feb - Met up with old secondary classmates for lunch! Been a couple of years (ten years for one of them) since i last met them. It was great to catch up
In the late afternoon, went to the Cottesloe beach, and i find it very nice, and somemore it is near! Auntie brought me to Point Walter too, to see the stretch of strolling path which leads into the river. Time to go home tonight and quite sad to leave them. Gonna miss you all!
All these while, i was considering Melbourne only as my migration target. After this trip, i like Perth very much too! Auntie kept asking me to move over there, and even starts to look out for house for me. Well, we will see in near future where i will end up at
I Learned and Applied RegularExpression Today
Scenario: I have a number of patterns that I need to check and replace in a string. I don't want to transverse the string and play with characters and length. What other option I have?
I want the library FAST!
My MSDN 2008 Library didn't install a direct link to launch help like the 2005 version. So i tweaked the 2005 shortcut and hoila, i have the shortcut for 2008 version. You like the F1, but i prefer this so i can easily answer people's query without opening the IDE
Create a new Shortcut and put this in the shortcut path (single line):
"%YOUR PROGRAM FILES%\Common Files\Microsoft Shared\Help 9\dexplore.exe"
/helpcol ms-help://MS.MSDNQTR.v90.en /LaunchNamedUrlTopic DefaultPage
coalescing null yield return
Ok the title means nothing at all, and they are not related too, except that they both are of C# family. As requested by Klaw, i am going to blog about it so that we all learn
Coalescing Null Operator
You are probably thinking what's that. Maybe it is just me, but it is one of the cool subtle features in C# that I just discovered today. Shame on me ya. Basically you all are familiar with this:
string myStrVarB = (myStrVarA == null) ? "null string" : myStrVarA
So with coalescing null operator (??), you can shortened it to:
string myStrVarB = myStrVarA ?? "null string"
Let's see, that is saving of 17 characters. Your boss sure will praise you for that
On a side note, in case you don't know, you can allow a scalar type to be null by using the nullable operator:
int? myInteger = null
yield return
Definition from MSDN: "Used in an iterator block to provide a value to the enumerator object or to signal the end of iteration". I assume you have previous knowledge with IEnumerable (or iterator), so i am not going into detail with it. The yield statement appears only inside an iterator block and returns the value that is expected by the calling foreach statement. Cut the theory and show me the example.
public static IEnumerable Power(int number, int exponent)
{
int counter = 0;
int result = 1;
while (counter++ < exponent)
{
result = result * number;
yield return result;
}
}
static void Main()
{
// Display powers of 2 up to the exponent 8:
foreach (int i in Power(2, 8))
{
Console.Write("{0} ", i);
}
}
There you go, another simple bit of my learning
Sharlinie and Black Magic
Read about this super interesting post here. My first reaction upon reading just the first paragraph: WTF. Why so much publicity on hiring bomoh to find the poor gal? And worst, 50 of them?! You can say i am heartless as they were trying all their mights, but this is really getting overboard. You know what, i seriously suspect there is consipracy behind this. Some political parties could have paid for this at the back. Why? Election is near mah. Why there isn't such big thing for Nurin before? Why now?
The bomohs predicted that she will return in 1 week. I really like to see theirs and public reaction if it doesn't come true. If they are so good, why cant they determine the exact location?! When the power combine, they are Captain Bomoh mah. They could even say there are 3 kidnappers whom used black magic to trap her. WTF again?! Black magic and kidnap? So what now, we should all learn some of the arts so we can fight back when necessary. And from this statement, shouldn't the police be searching all bomohs in Malaysia (since they know black magic)?
Not that i am pessimistic about Sharlinie safety but not this method alright. Even if you do, keep it low. Headline man, what is the paper thinking? Is Malaysia really that boring till we need to use such news for headline?
Begin Invoke..End Invoke
First post in the category
This group is mainly to share coding stuff that is out of layman term. If you are not programmer, stay away at all cost!
Found this very easy to read yet informative post on Asynchronous Method Invocation. I admit that what i know about .NET is merely the tiny tip of what the iceberg can offer. Oh hell, life is long and learning is life long, ain't it?
Here's a summary of what i learnt:
- MethodInvoke - built in delegate in .NET that allow you to call a method that takes in no parameter
- BeginInvoke - execute the target function and return to caller instantly
- ThreadPool in .NET has a limit of 25. Avoid 'starving' the pool else be fined by performance loss
- EndInvoke - return proper values/parameters from the previous function call
- IAsyncResult - log of all datas (exceptions, return value, output parameters, etc) after execution is completed
- AsyncCallBack - used when you don't wish to wait for completion of the execution (that's the purpose of asynchronous call bah)
- Command Pattern (ICommand interface) - rather than BeginInvoke, EndInvoke, CallBack everywhere in your Main(), apply this pattern to group calls together.
That's all folks. I might not have explained everything properly, so best for you to read it yourself.
I am on Facebook
You would be saying, "What? You are so out man! We've been fooling around in there for ages". Alright alright, i know i am slow. The main reason that i registered for it was to view photos from my ex-colleagues. Only after i logged in, i found tonnes of my friends in there (who weren't active in such portal before).
Frankly, i don't like Facebook much, except that it is responsive and hell lots of friends are in there. They have too much of those 'applications' which complicate the whole thing. Whenever i browse profiles, i am only interested in the top section, where the biodata and photos links are. I still prefer Friendster style where things are kept to simplest form. Oh maybe i am not into 'socializing through the web' now, so i don't see the usefulness of those goodies.
Anyway, if you want to, you can find me in there with email digital.ric@gmail.com. Happy Facebook-ing!
20080119 Today’s Photo
Dad and Mum at Rose Valley of Cameron Highland.
| Retro Colors | Stand out | Original |