Loop through object array

Posted on 2011-05-11
Last Modified: 2012-05-11
Is there a way to loop through "myObject" to see what it contains?

Please provide C# source code.

  object myObject = new
                        LOGIN = username,
                        PASSWORD = password,
                        FIRSTNAME = BillingInfo.FirstName,
                        LASTNAME = BillingInfo.LastName,
                        ADDRESS1 = BillingInfo.Address1,
                        ADDRESS2 = BillingInfo.Address2,
                        CITY = BillingInfo.City,
                        STATE = BillingInfo.State,
                        ZIP = BillingInfo.Zip,
                        COUNTRY = BillingInfo.Country,
                        EMAIL = BillingInfo.Email,
                        DAYPHONE = BillingInfo.DayPhone,
                        EVEPHONE = BillingInfo.NightPhone,
                        SHIPTONAME = ShippingInfo.FirstName,
                        SHIPTOADDRESS1 = ShippingInfo.Address1,
                        SHIPTOADDRESS2 = ShippingInfo.Address2,
                        SHIPTOCITY = ShippingInfo.City,
                        SHIPTOSTATE = ShippingInfo.State,
                        SHIPTOZIP = ShippingInfo.Zip,
                        SHIPTPCOUNTRY = ShippingInfo.Country,
                        SHIPINSTRUCTIONS = ShippingInfo.ShipInstructions,
                        EMAILOPTIN = emailOptIn,
                        NEWMETHOD = "NEWMETHOD"

Open in new window

Question by:knowlton
    LVL 13

    Accepted Solution

    LVL 5

    Author Comment

    I had not looked at that.

    What would be the C# equivalent.

    Dim myType As Type = GetType(Person)
            Dim properties As System.Reflection.PropertyInfo() = myType.GetProperties()
            For Each p As System.Reflection.PropertyInfo In properties

    This many points is for FUNCTIONING C# code provided by the expert, as I requested.
    LVL 5

    Author Comment

    In the meantime, my attempt to convert gets lots of errors.

    Type myType = GetType(myObject);
                        System.Reflection.PropertyInfo properties = System.Reflection.PropertyInfo();
                        properties myType.GetProperties();
                        foreach(System.Reflection.PropertyInfo p in properties)

    Open in new window

    Error      157      'System.Reflection.PropertyInfo' is a 'type', which is not valid in the given context      C:\KnowltonCWS\KnowltonCWS\KnowltonCWS\CWS\Website\Campus Webstore\UserControls\AddOrEditUser.ascx.cs      341      65      Campus Webstore
    Error      16      ; expected      C:\KnowltonCWS\KnowltonCWS\KnowltonCWS\CWS\Website\Campus Webstore\UserControls\AddOrEditUser.ascx.cs      342      38      Campus Webstore
    Error      17      ; expected      C:\KnowltonCWS\KnowltonCWS\KnowltonCWS\CWS\Website\Campus Webstore\UserControls\AddOrEditUser.ascx.cs      342      39      Campus Webstore
    Error      159      A local variable named 'myType' is already defined in this scope      C:\KnowltonCWS\KnowltonCWS\KnowltonCWS\CWS\Website\Campus Webstore\UserControls\AddOrEditUser.ascx.cs      342      32      Campus Webstore
    Error      162      foreach statement cannot operate on variables of type 'System.Reflection.PropertyInfo' because 'System.Reflection.PropertyInfo' does not contain a public definition for 'GetEnumerator'      C:\KnowltonCWS\KnowltonCWS\KnowltonCWS\CWS\Website\Campus Webstore\UserControls\AddOrEditUser.ascx.cs      343      21      Campus Webstore
    Error      15      Invalid expression term '.'      C:\KnowltonCWS\KnowltonCWS\KnowltonCWS\CWS\Website\Campus Webstore\UserControls\AddOrEditUser.ascx.cs      342      38      Campus Webstore
    Error      156      No overload for method 'GetType' takes 1 arguments      C:\KnowltonCWS\KnowltonCWS\KnowltonCWS\CWS\Website\Campus Webstore\UserControls\AddOrEditUser.ascx.cs      340      35      Campus Webstore
    Error      160      Only assignment, call, increment, decrement, and new object expressions can be used as a statement      C:\KnowltonCWS\KnowltonCWS\KnowltonCWS\CWS\Website\Campus Webstore\UserControls\AddOrEditUser.ascx.cs      342      32      Campus Webstore
    Error      161      The name 'GetProperties' does not exist in the current context      C:\KnowltonCWS\KnowltonCWS\KnowltonCWS\CWS\Website\Campus Webstore\UserControls\AddOrEditUser.ascx.cs      342      39      Campus Webstore
    Error      158      The type or namespace name 'properties' could not be found (are you missing a using directive or an assembly reference?)      C:\KnowltonCWS\KnowltonCWS\KnowltonCWS\CWS\Website\Campus Webstore\UserControls\AddOrEditUser.ascx.cs      342      21      Campus Webstore
    LVL 9

    Assisted Solution


    have you tried this?

            Type myType = typeof(Person);
            System.Reflection.PropertyInfo[] properties = myType.GetProperties();
            foreach (System.Reflection.PropertyInfo p in properties)
    LVL 5

    Author Closing Comment

    My eventual solution:

     foreach (object e in myObject.ToString().ToArray())
                            a += e.ToString() + "   ";

    Open in new window


    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
    A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now