none
Getting the values from an Object RRS feed

  • Question

  • Hi all,

    I have a rather silly question, but after a lot of searching on the forums and the Web, I was not able to find an answer yet:

    When one step through the code lines in Visual Studio, you can hover your mouse over a variable to see its values eg:
         variablename | {ID=26, Name=JOE, Surname=SOAP, Title=MR}

    If it is a collection then you can also see the index of the particular item in the collection eg:
         [2] = {ID=26, Name=JOE, Surname=SOAP, Title=MR}

    If you then click on the + sign, the individual fields and values are displayed eg:
        ID             26
        Name        JOE
        Surname   SOAP
        Title          MR

    I know about type casting etc., but what if you just have a variable for example of type Object, how do I get the field values typically like the way they display it in Visual Studio during runtime ?
    I need some way to for example refer to the field value eg. pers.Name or it would even be helpful if I can convert it to a multi-dimensional array, so for example pers[2][1] would refer to the Name of the 2nd record in the case of a collection. At the moment I do a foreach loop and convert the variable to a string and try to read the field values from the string, but it is a pain.

    Thanks a lot in advance for any help.

    Tuesday, October 13, 2009 9:59 AM

Answers

  • Hi,

    May be this can be an idea.

    A class representing the Field and Value.
    public class FieldValue
            {
                public string FieldName { set; get; }
                public object Value{get;set;}
            }
    One static function to create the list of FieldValue class
    public static List<FieldValue> GetFields(object obj)
            {
                if (obj != null)
                {
                    List<FieldValue> FieldValueList = (from field in obj.GetType().GetProperties()
                            select new FieldValue { FieldName = field.Name, Value = field.GetValue(obj, null) }).ToList<FieldValue>();
                    return FieldValueList;
                }
                return null;
            }
    You can use it like this,
    List<FieldValue> FieldValueList = GetFields(yourobject);
                Console.WriteLine(FieldValueList[0].Value.ToString());


    Regards,
    Vinil;
    Tuesday, October 13, 2009 11:03 AM

All replies

  • Not sure what you mean, Object doesn't have any fields.  I think you are talking about debugger visualizers.

    Hans Passant.
    Tuesday, October 13, 2009 10:13 AM
    Moderator
  • Thanks for the quick response. No, I am not referring to writing debugger visualizers, I am referring to something much simpler than that. Let me give you a practical example :
    I am writing a WPF application and using LINQtoSQL to query the database. In one of my queries I use LINQToSQL with joins to a lot of other tables and fields eg:
        var allemps = from flds in DataCont.Employees
                               select new
                               {
                                    flds.Name, flds.Surname, flds.ID, flds.Address.Code  ..... etc ... etc .....
                               } 

    In my UI I have a filter mechanism based on the above list of fields. To get the filtered result, I build a DynamicLINQ query on the above variable eg.
        var filtered = allemps.Where(where_part).Select("New(" + field_list + ")");

    I then bind the filtered variable to a WPF datagrid eg. grdData.ItemsSource = filtered

    Now, this is where my problem comes in, If i iterate through the items in the grid like this:

                for (int i = 0; i < grdData.Items.Count; i++)
                {

                    object r = grdData.Items[i];

                }
    if I hover over the r variable during runtime, I see data that looks for example like this:

         r | {ID=26, Name=JOE, Surname=SOAP, Title=MR}

    I want to know if there is an easy out-of-the-box way to cast or convert r into some kind of object so that I can refer to the individual fields a lot easier. For example, if I want to extract the Surname from the above string, it is a bit of a mission, it would have been nice if I could do something like r[3] to get me the surname value, or perhaps something like r("Surname") or r["Surname"] to get to the value of "SOAP". Remember that in this example, my filtered list is dynamic in terms of fields too, some users may only want to see the Surname, Title fields and other users may want to see more fields.

    Any help or pointer will be very much appreciated.

    Tuesday, October 13, 2009 10:49 AM
  • Hi,

    May be this can be an idea.

    A class representing the Field and Value.
    public class FieldValue
            {
                public string FieldName { set; get; }
                public object Value{get;set;}
            }
    One static function to create the list of FieldValue class
    public static List<FieldValue> GetFields(object obj)
            {
                if (obj != null)
                {
                    List<FieldValue> FieldValueList = (from field in obj.GetType().GetProperties()
                            select new FieldValue { FieldName = field.Name, Value = field.GetValue(obj, null) }).ToList<FieldValue>();
                    return FieldValueList;
                }
                return null;
            }
    You can use it like this,
    List<FieldValue> FieldValueList = GetFields(yourobject);
                Console.WriteLine(FieldValueList[0].Value.ToString());


    Regards,
    Vinil;
    Tuesday, October 13, 2009 11:03 AM
  • Fantastic, thanks a lot VinilV !!!
    • Marked as answer by Anton Marais Tuesday, October 13, 2009 2:24 PM
    • Unmarked as answer by nobugzModerator Tuesday, October 13, 2009 2:41 PM
    Tuesday, October 13, 2009 2:24 PM