tomcattyy
asked on
loop through a object property in asp.net c#
Hello there,
I have an array create from read the url parameter to identify what field has been changed in the previous form. The Object has field like acct_no,acct_name,acct_ema il,add1,ad d2....
I want to loop through the array and pull the matching field value from the object.
org and aft are two objects
arInfo is the array contains the changed field name
for (int x = 0; x < arInfo.Length; x++)
{
string field=arInfo [x];
listBody = "original: " + org.fieldl + " - change to: " + aft.fieldl+"<br>";
}
above code have error message saying object don't have the property "field". How can I concat the filed name to pull property like org.acct_name or org_add1 ?
Appreciate your help.
I have an array create from read the url parameter to identify what field has been changed in the previous form. The Object has field like acct_no,acct_name,acct_ema
I want to loop through the array and pull the matching field value from the object.
org and aft are two objects
arInfo is the array contains the changed field name
for (int x = 0; x < arInfo.Length; x++)
{
string field=arInfo [x];
listBody = "original: " + org.fieldl + " - change to: " + aft.fieldl+"<br>";
}
above code have error message saying object don't have the property "field". How can I concat the filed name to pull property like org.acct_name or org_add1 ?
Appreciate your help.
ASKER
Thanks, Joep
I think that's the way to go just I am not familiar with the dictionary creation, below is my class, can you point me where should I change to create the dictionary.
I think that's the way to go just I am not familiar with the dictionary creation, below is my class, can you point me where should I change to create the dictionary.
ASKER
sorry forget the class code
[Serializable ]
public class userChange
{
private string _corr;
private string _office;
private string _acct_no;
private string _acct_name;
private string _company;
private string _marital;
private string _add1;
private string _add2;
private string _city;
private string _zip;
private string _country;
private string _phone;
private string _fax;
private string _email;
private string _joinName;
private string _mailadd1;
private string _mailadd2;
private string _mailcity;
private string _mailst;
private string _mailzip;
private string _mailcountry;
private string _request_IP;
public userChange()
{
}
public string corr
{
set { this._corr = value; }
get { return this._corr ;}
}
public string office
{
set { this._office = value; }
get { return this._office; }
}
public string acct_no
{
set { this._acct_no = value; }
get { return this._acct_no; }
}
public string acct_name
{
set { this._acct_name = value; }
get { return this._acct_name; }
}
public string company
{
set { this._company = value; }
get { return this._company; }
}
public string marital
{
set { this._marital = value; }
get { return this._marital; }
}
public string add1
{
set { this._add1 = value; }
get { return this._add1; }
}
public string add2
{
set { this._add2 = value; }
get { return this._add2; }
}
public string city
{
set { this._city = value; }
get { return this._city; }
}
public string zip
{
set { this._zip = value; }
get { return this._zip; }
}
public string country
{
set { this._country = value; }
get { return this._country; }
}
public string phone
{
set { this._phone = value; }
get { return this._phone; }
}
public string fax
{
set { this._fax = value; }
get { return this._fax; }
}
public string email
{
set { this._email = value; }
get { return this._email; }
}
public string mailadd1
{
set { this._mailadd1 = value; }
get { return this._mailadd1; }
}
public string mailadd2
{
set { this._mailadd2 = value; }
get { return this._mailadd2; }
}
public string mailcity
{
set { this._mailcity = value; }
get { return this._mailcity; }
}
public string mailst
{
set { this._mailst = value; }
get { return this._mailst; }
}
public string mailzip
{
set { this._mailzip = value; }
get { return this._mailzip; }
}
public string mailcountry
{
set { this._mailcountry = value; }
get { return this._country; }
}
public string request_IP
{
set { this._request_IP = value; }
get { return this._request_IP; }
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hello Joep,
I implement the new object but got an error, is it just the key name passed in is different than the one defined? Also I attached the new code, hope I did not make something wrong. Thanks.
The given key was not present in the dictionary.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Collections.Generic .KeyNotFou ndExceptio n: The given key was not present in the dictionary.
Source Error:
Line 132: public string this[string key]
Line 133: {
Line 134: get { return Values[key]; }
Line 135: set { Values[key] = value; }
Line 136: }
Source File: d:\Web\App_Code\userchange .cs Line: 134
I implement the new object but got an error, is it just the key name passed in is different than the one defined? Also I attached the new code, hope I did not make something wrong. Thanks.
The given key was not present in the dictionary.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Collections.Generic
Source Error:
Line 132: public string this[string key]
Line 133: {
Line 134: get { return Values[key]; }
Line 135: set { Values[key] = value; }
Line 136: }
Source File: d:\Web\App_Code\userchange
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
/// <summary>
/// Summary description for user
/// </summary>
[Serializable ]
public class userChange
{
private Dictionary<string, string> Values;
public userChange()
{
Values = new Dictionary<string, string>();
}
public string corr
{
set { Values ["_corr"] = value; }
get { return Values["_corr"] ;}
}
public string office
{
set { Values["_office"] = value; }
get { return Values["_office"]; }
}
public string acct_no
{
set { Values["_acct_no"] = value; }
get { return Values["_acct_no"]; }
}
public string acct_name
{
set { Values["_acct_name"] = value; }
get { return Values["_acct_name"]; }
}
public string company
{
set { Values["_company"] = value; }
get { return Values["_company"]; }
}
public string marital
{
set { Values["_marital"] = value; }
get { return Values["_marital"]; }
}
public string add1
{
set { Values["_add1"] = value; }
get { return Values["_add1"]; }
}
public string add2
{
set { Values["_add2"] = value; }
get { return Values["_add2"]; }
}
public string city
{
set { Values["_city"] = value; }
get { return Values["_city"]; }
}
public string zip
{
set { Values["_zip"] = value; }
get { return Values["_zip"]; }
}
public string country
{
set { Values["_country"] = value; }
get { return Values["_country"]; }
}
public string phone
{
set { Values["_phone"] = value; }
get { return Values["_phone"]; }
}
public string fax
{
set { Values["_fax"] = value; }
get { return Values["_fax"]; }
}
public string email
{
set { Values["_email"] = value; }
get { return Values["_email"]; }
}
public string mailadd1
{
set { Values["_mailadd1"] = value; }
get { return Values["_mailadd1"]; }
}
public string mailadd2
{
set { Values["_mailadd2"] = value; }
get { return Values["_mailadd2"]; }
}
public string mailcity
{
set { Values["_mailcity"] = value; }
get { return Values["_mailcity"]; }
}
public string mailst
{
set { Values["_mailst"] = value; }
get { return Values["_mailst"]; }
}
public string mailzip
{
set { Values["_mailzip"] = value; }
get { return Values["_mailzip"]; }
}
public string mailcountry
{
set { Values["_mailcountry"] = value; }
get { return Values["_mailcountry"]; }
}
public string request_IP
{
set { Values["_request_IP"] = value; }
get { return Values["_request_IP"]; }
}
public string this[string key]
{
get { return Values[key]; }
set { Values[key] = value; }
}
}
ASKER
Thanks Joep, It's the class key name not matching the ID from the page, I changed and it's working now. Appreciate your help.
ASKER
Perfect and precise
If you do not set the values initially, adjust your class to look like this
public string corr
{
set { SetValue("_corr", value); }
get { return GetValue("_corr"); }
}
private void SetValue(string key, string value)
{
// check if the key exists.
if (!Values.ContainsKey(key))
Values.Add(key, "");
Values[key] = value;
}
private string GetValue(string key)
{
if (!Values.ContainsKey(key))
Values.Add(key, "");
return Values[key];
}
If you stored the values for each field inside the object in a hashtable, dictionary or something, you could provide an indexer property. See the attached code snippet.
Open in new window