Tom Knowlton
asked on
Loop through object array
Is there a way to loop through "myObject" to see what it contains?
Please provide C# source code.
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"
};
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
In the meantime, my attempt to convert gets lots of errors.
Error 157 'System.Reflection.Propert yInfo' is a 'type', which is not valid in the given context C:\KnowltonCWS\KnowltonCWS \KnowltonC WS\CWS\Web site\Campu s Webstore\UserControls\AddO rEditUser. ascx.cs 341 65 Campus Webstore
Error 16 ; expected C:\KnowltonCWS\KnowltonCWS \KnowltonC WS\CWS\Web site\Campu s Webstore\UserControls\AddO rEditUser. ascx.cs 342 38 Campus Webstore
Error 17 ; expected C:\KnowltonCWS\KnowltonCWS \KnowltonC WS\CWS\Web site\Campu s Webstore\UserControls\AddO rEditUser. ascx.cs 342 39 Campus Webstore
Error 159 A local variable named 'myType' is already defined in this scope C:\KnowltonCWS\KnowltonCWS \KnowltonC WS\CWS\Web site\Campu s Webstore\UserControls\AddO rEditUser. ascx.cs 342 32 Campus Webstore
Error 162 foreach statement cannot operate on variables of type 'System.Reflection.Propert yInfo' because 'System.Reflection.Propert yInfo' does not contain a public definition for 'GetEnumerator' C:\KnowltonCWS\KnowltonCWS \KnowltonC WS\CWS\Web site\Campu s Webstore\UserControls\AddO rEditUser. ascx.cs 343 21 Campus Webstore
Error 15 Invalid expression term '.' C:\KnowltonCWS\KnowltonCWS \KnowltonC WS\CWS\Web site\Campu s Webstore\UserControls\AddO rEditUser. ascx.cs 342 38 Campus Webstore
Error 156 No overload for method 'GetType' takes 1 arguments C:\KnowltonCWS\KnowltonCWS \KnowltonC WS\CWS\Web site\Campu s Webstore\UserControls\AddO rEditUser. 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 \KnowltonC WS\CWS\Web site\Campu s Webstore\UserControls\AddO rEditUser. ascx.cs 342 32 Campus Webstore
Error 161 The name 'GetProperties' does not exist in the current context C:\KnowltonCWS\KnowltonCWS \KnowltonC WS\CWS\Web site\Campu s Webstore\UserControls\AddO rEditUser. 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 \KnowltonC WS\CWS\Web site\Campu s Webstore\UserControls\AddO rEditUser. ascx.cs 342 21 Campus Webstore
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);
}
Error 157 'System.Reflection.Propert
Error 16 ; expected C:\KnowltonCWS\KnowltonCWS
Error 17 ; expected C:\KnowltonCWS\KnowltonCWS
Error 159 A local variable named 'myType' is already defined in this scope C:\KnowltonCWS\KnowltonCWS
Error 162 foreach statement cannot operate on variables of type 'System.Reflection.Propert
Error 15 Invalid expression term '.' C:\KnowltonCWS\KnowltonCWS
Error 156 No overload for method 'GetType' takes 1 arguments C:\KnowltonCWS\KnowltonCWS
Error 160 Only assignment, call, increment, decrement, and new object expressions can be used as a statement C:\KnowltonCWS\KnowltonCWS
Error 161 The name 'GetProperties' does not exist in the current context C:\KnowltonCWS\KnowltonCWS
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
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
My eventual solution:
foreach (object e in myObject.ToString().ToArray())
{
a += e.ToString() + " ";
}
ASKER
What would be the C# equivalent.
Dim myType As Type = GetType(Person)
Dim properties As System.Reflection.Property
For Each p As System.Reflection.Property
Response.Write(p.Name)
Next
This many points is for FUNCTIONING C# code provided by the expert, as I requested.