Link to home
Start Free TrialLog in
Avatar of Tom Knowlton
Tom KnowltonFlag for United States of America

asked on

Loop through object array

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

ASKER CERTIFIED SOLUTION
Avatar of Rick
Rick

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Tom Knowlton

ASKER

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
            Response.Write(p.Name)
        Next

This many points is for FUNCTIONING C# code provided by the expert, as I requested.
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)
                    {
                        Response.Write(p.Name);
                    }

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
SOLUTION
Avatar of gery128
gery128
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
My eventual solution:

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

Open in new window