hi,
i hqve posted a thread similar to this. but i don think i have received the answer becuase my post is not clear.
Therefore i i'l try to explain this through some code:
Assume a class Customer has the following code:
public class Customer
{
public String CustNo { get; set; }
public String CustName { get; set; }
public String Address { get; set; }
public Customer(string num, string name, string address)
{
CustNo = num;
CustName = name;
Address = address;
}
}
I need to add an update method to the class that will update the properites of the class. But the update should receive a Collection as a parameter. In that collection the indexes should be not 0, 1,2, 3.... but the names of the properties of the Customer class.
Such as 0th index name should be "CustNo", 1th index name should be "CustName"....so forth.
Therefore in the update method i could compare the current value a property e.g.: CustNo agaist the value index name "CustNo"
The algorithm for the update() as follows:
public CUstomer
{
public Customer(string num, string name, string address)
{
CustNo = num;
CustName = name;
Address = address;
}
//Collection type is to indicate col is a collection for your
//understanding only. But the actual method should take a
//collection as a arguemtn and it's index names should //be "CustNo", "CustName" and "Address"
public void update(Collection col)
{
if ( col["CustNo"] == this.CustNo )
{ // do calculation....}
}
}
How do i create such collections?

1 answers
It's still not clear!
Your 'update' method is not updating the values of the properties - it's just comparing them with the corresponding values in the collection and doing a calculation if they're equal.
answered 2 years ago by:
17279
I think what he's trying to do is create objects which can access properties by name via an indexer. Then have a collection of those objects.
Since the example has only a few properties, I'd recommend using a switch block for and return the appropriate property. Reflection is another option but I generally avoid it if possible. I gave you an example back on the original thread on how to create an indexer.
answered 2 years ago by:
538
Possibly, though I wondered whether he was just looking for a way of updating the properties in bulk which you could do easily enough using a switch:
public void Update(Dictionary<string, string> dict)
{
foreach (string key in dict.Keys)
{
switch(key)
{
case "CustNo":
CustNo = dict[key];
break;
case "CustName":
CustName = dict[key];
break;
case "Address":
Address = dict[key];
break;
default:
// do nothing
break;
}
}
}
answered 2 years ago by:
17279
Thanks problem solved : )
answered 2 years ago by:
0
This post was imported from csharpfriends, if you have a similiar question please ask it again.
All previous members have been migrated, hope you enjoy the new platform!