I need help with serial numbers. I need a serial number to be generated based on the department and the month. departmentyearmonth_serial number
KL2103_01, KM2103_01, KL2103_02...
textBox1.Text = textBox2.Text dtim.ToString("yyMM") serial number;
What should the cod for serial number look like?
Thank you for your help?
CodePudding user response:
public string GetSerialNumber(string department, string serialNumber)
{
return department DateTime.Now.ToString("yyMM") "_" serialNumber;
}
then when you call it:
textBox1.Text = GetSerialNumber("KL",01);
you could also create the possible departments as enums to avoid typos and depending on your serial number generation access it in the GetSerialNumber method and therefore eliminate the second method parameter
edit: You don't know how to generate the number part? you could keep the last used number in a variable and increment it. if you need a separate number for each department you could use an int array together with the enum I already suggested:
enum Department
{
KL,
KM
}
int[] ids = new int[]{0,0}
then you can change the method to:
public string GetSerialNumber(Department department)
{
return department.ToString() DateTime.Now.ToString("yyMM") "_" ( ids[department]);
}
to get persistent serail numbers after restarting the application you have to save and load the values somewhere
CodePudding user response:
Global in your app: Make a class/struct with department string and serial index (int) as member. Make a list of these classes/structs. Add a variable for the current month. All these variables are to be stored somewhere at disk/net (depending if it's running at only one computer or at different of the network).
If you have to create a number first check if the current month has changed by comparing with the variable. If it has changed create a new list, store the new month at the variable. Then search the department string in the list. If it's not found add a new entry to the list with the given department string and 1 as index. If it was found increase the index. Then create the string like petule.08 told it with the function GetSerialNumber and the new index.