I'm new in C#. I'm trying to find a method to have an "easy" access to my customers, example:
allCustomers[44][Name] -> gives me the name of the customer no 44
allCustomers[30][Age] -> gives me the age of the customer no 33
allCustomers is constructed as follows:
int customerNo = 44;
string name = "Gates";
string prename = "Bill";
int age = 30;
float revenue = 234.234F
Dictionary
Dictionary
allCustomers.Add(CustomerNo, customer.Add("Name", name);
allCustomers.Add(CustomerNo, customer.Add("Prename ", prename );
allCustomers.Add(CustomerNo, customer.Add("Age", age.ToString());
allCustomers.Add(CustomerNo, customer.Add("Revenue ", revenueToString());
My problem now is, that I have to convert all variables into string and vice versa, for example "age"
Question: Is there a better way/design to solve this problem ?
I would be very thankful if I could get as many suggestions.
thanks mike
mikePosted Jan 1, 2009, 4:10 PM
That's exactly what I'm looking for !
Thanks a lot
mike
Carl SchraderPosted Jan 1, 2009, 4:06 PM
Mike,
I would encapsulate all the variables (customerNo, name, prename, etc.) in a “Customer” object (class) and expose the members through properties. I would then use a generic list (System.Collections.Generic.List) that would contain the list of Customer objects.
IE:
public class Customer
{
private float revenue;
private int customerNo;
private int age;
private string name;
private string prename;
public Customer()
{
}
public float Revenue
{
get { return revenue; }
}
public int CustomerNumber
{
get { return customerNo; }
}
public int Age
{
get { return age; }
}
public string FirstName
{
get { return prename; }
}
public string LastName
{
get { return name; }
}
public static void Main(String[] args)
{
List<Customer> myCustList = getCustomerList(); // Function returns customer list
string fName = myCustList[44].FirstName; // gives me the name of the customer no 44
int iAge = myCustList[30].Age; // gives me the age of the customer no 30
}
}
Carl