Reputation: 3
I need to find the lowest customer id in the array and increase it by 1.
So I need the for cycle to search for the biggest id (0 by default) and increase by 1 every time ( create a new object of the customer class.
I really don't know how to implement the "i" value in order to make the cycle search for the id...
My model:
public class CustomerModel
{
public int IDCustomer {get; set;}
public string LastNameCustomer {get; set;}
public string FirstNameCustomer {get; set;}
public string AdressCustomer {get; set;}
}
My algorithm attempt:
CustomerModel[] MemoryCustomers = new CustomerModel[9];
OrderModel[] MemoryOrders = new OrderModel[9];
public string CreateCustomer(CustomerModel model)
{
CustomerModel NewCustomer = new CustomerModel();
for (int i = 0; i < 10; i++)
{
//What to put here
}
}
Upvotes: 0
Views: 196
Reputation: 19101
Don't do that.
You should instead try to get the next id for a customer from some external provider. If you're using a database system, that can generate indices for you - you just need to define a counter or ID-generator or some such thing in the DBMS. How that is done depends on which DB you are using.
Otherwise, you could create your own method that you can call each time you need a new id; something like:
newCustomer.IDCustomer = GetNextId();
If doing that, make sure the method can never return any ID more than once, and you should be safe. There are several ways to do this; one is to constantly increase a number. Another is to use an UUID or GUID:
var id = Guid.NewGuid().ToString() // Generate a universally unique ID-string
The latter is especially useful if you want ID's that appear random, so that knowing one or two ID's won't make it easy to guess others (a common security problem / weakness in many apps).
Update:
Ok, ok: If you really want to simply get the next available ID as an int, based on existing int's, then try this:
private int GetNextId(){
// Create a list for easy handling with LINQ:
List<CustomerModel> customers = new List<CustomerModel)(MemoryCustomers);
// Select only ids, and only the highest:
int highestExistingId = customers
.Select(cust => cust.IDCustomer)
.Max();
return highestExistingId + 1;
}
If I've understood your question correctly now after re-reading it, I believe you want to update the lowest ID in your array, and replace it with a new customer,with the new ID - is that correct?
If so, try:
List<CustomerModel> customers = new List<CustomerModel)(MemoryCustomers);
var lowestId = customers.Select(cust => cust.IDCustomer).Min();
for (int i = 0; i < 10; i++)
{
// Identify the customer with that lowest id...
if(MemoryCustomers[i].IDCustomer == lowestId)
{
// ... replace him with the new one:
MemoryCustomers[i] = NewCustomer;
}
}
Upvotes: 0
Reputation: 51
This may not be what you are searching for but it should work more consistent.
Add something like this to your Customer-Class:
private static int last_gen_id = -1;
public static int Gen_ID {
get {
last_gen_id++;
return last_gen_id;
}
}
And for every id, you want to generate, you just call Customer.Gen_ID
and you got yourself a unique id. This will not work with threading, so be warned ;)
Upvotes: 0
Reputation: 296
it is not a good practice to do that, you can use "SQL identity" if dealing with database. or you can use type GUID to generate unique identifier for each customer.
Upvotes: 2