Posts

Showing posts with the label csharp

NSString's Split and Join crash course

Split a NSString is using the NSString instance method " componentsSeparatedByString " NSString * mystring = @"coding;is;kind;of;magic" ; NSArray * mysplitedstring = [mystring componentsSeparatedByString : @";" ]; The above code will produce an array containing the following element: coding is kind of magic To join an array of NSString back to a single NSString, use the NSArray instance method " componentsJoinedByString " NSString * mystring = [mysplitedstring componentsJoinedByString : @";" ]; That will give you back the NSString "coding;is;kind;of;magic"

Using bitwise operation OR & AND on integer for validation

Today I'm going to revisit bitwise OR (|) and AND (&) operation on integer specifically for role based security checking scenario. Scenario : There are few reason why bitwise operation on integer is needed in an application, but more frequently seen to be use for validation for e.g. user's role validation. This is a very useful scenario, the application stores only one integer on the database to represent all the role that is assigned to that user. And on the application, user role is usually represented as Enum (Enum is a kind of integer). Lets get into some coding. Following is small C# program I wrote on LINQPad. void Main() { // PART (A) Define user role int Public = 1; int Sales = 2; int Marketing = 4; int Engineering = 8; // PART (B) Assign some role to user. Assigned role is stored as an integer in the db of the user table. int currentUser1 = Sales | Public; int currentUser2 = Marketing | Public; int currentUser3 = Sales | Marketing | Publ...

Copy all public properties from an object to another object

I often ran into situation where I need to copy all the value of an object into another object, this normally happen in ORM related project where you need the value to be copy from an entity to an entity retrived from db (ActiveRecord) kind of entity, so I created a simple extension method to facilitate this action. /// /// Copy the speficied public properties from source to target. /// /// Type of object. /// Target object to copy to./// Source object to copy from./// Target object with each public properties copied from source object. public static T CopyFrom (this T target, object source) { var sourceObjType = source.GetType(); //loop through each public properties in the type foreach (var targetProperty in target.GetType().GetProperties()) { var sourceProperty = sourceObjType.GetProperty(targetProperty.Name); if (sourceProperty != null) // match found { // check types and take care of Nullable type. var sourceType = Nullable.GetUnderlyingType(sourceProp...

Revise C#'s Constructor Overloading

When overloading constructor in C# I always tends to forgot what is the execution cycle during runtime. So I made a quick program with LinqPad to refresh my mind. 1st I create a simple class Item with multiple constructor overloading each others. class Item { private int i = 1 ; public Item (){ this .i = i* 10 ; Console.WriteLine( "Item - constructor 1 executed. i = " + this .i); } public Item ( int i) : this (){ this .i *= i; Console.WriteLine( "Item - constructor 2 executed. i = " + this .i); } public Item ( string a) : this ( 5 ){ Console.WriteLine( "Item - constructor 3 executed. i = " + this .i); } } then I create a Main function to create new Item. // Constructor Overloading void Main () { Console.WriteLine( "START" ); Console.WriteLine( "--------------" ); var i = new Item(); Console.WriteLine( "--------------" ); ...