Monday, March 14, 2011

Looping through Object's properties in C Sharp

Loop to Find All Properties Info of an Object In C Sharp..
Loop to Find All Properties Info of an Object In C#.

    Have you ever tried to make a dynamic test page? To build and HTML element contains all the properties of a specific .Net object?

    Well, I encountered this problem couple of days ago and while searching Google I barely found good matches.  So I decided that for my sake, and yours, I will simply post a short explanation about how to do it, followed by a quick and simple example of how to loop though object properties in C#.

    First I'll provide a short explanation about Reflection:
    Reflection enables you to discover an object's properties, methods, events and properties values at run-time.  Reflection can give you a full mapping of an object (well, it's public data).

    In order to use reflection, and to loop through the object's properties, we'll first have to import the namespace:
    using System.Reflection;

    Now, lets say I have an object called User and this object has 2 public properties:
    1. UserID.
    2. Name.
    My code will look something like this:

    int testID = 123456;
    User usr = new User(testID); // this is constructor to get user by ID
    StringBuilder sb = new StringBuilder();

    PropertyInfo[] properties = user.GetType().GetProperties();
    foreach (PropertyInfo pi in properties)
    {
        sb.Append(
            string.Format("Name: {0} | Value: {1}", 
                    pi.Name, 
                    pi.GetValue(user, null)
                )
        );
    }
    divResults.InnerText = sb.ToString();

    This little piece of code will fill your HTML element with the properties names and values.

    Have Fun,
    Elad Shalom,
    CTO at ITweetLive.com

    Sunday, March 13, 2011

    JavaScript Looping Through Object Properties

    Looping Through JSON Object Properties in JavaScript.

    My previous post contained a small piece of code explaining how to loop through object properties with the Reflection namespace in C#.

    In this post I'd like to explain how do loop through JavaScript object.

    Let's create an object in JavaScript:

    var myObject = {
        Name: "Elad",
        LastName: "Shalom",
        Age: 26,
        Kids: ["Daniel"]
    };

    This object contains 4 properties:
    1. Name (string)
    2. LastName (string)
    3. Age (int)
    4. Kids (array)
    Now  for the loop part:

    function LoopThroughProperties(obj)
    {
        for (var propName in obj)
        {
            alert(propName + ": " + obj[propName]);
        }
    }

    The function will receive an object and loop through all of it's properties.

    I'd like to take 2-4 more lines to explain a bit about the for syntax I used.
    Those of you who write in C# / VB / Java will find it very similar to foreach loop in JavaScript.

    Since an object in JavaScript is a form of array, I can easily call every one of it's properties the same way (almost) I'd call them when looping through array.
    This type of forach loop in JavaScript is also very useful when going through a hash table.  since we don't know the numbers the hash contains, we can simply loop through it.

    Thanks,
    Elad Shalom,
    CTO at ITweetLive.com