|
If I understood correctly, you can do like this:
string receiptCode = "Rcp2008";
int lastReceiptNumber = 248;
int receiptDisplayDigits = 8;
string receiptString = lastReceiptNumber.ToString().PadLeft(receiptDisplayDigits, '0');
string fullReceiptCode = string.Format("{0}-{1}", receiptCode, receiptString);
EDIT: expanded taking into account your second post.
|
|
|
|
|
it worked fine but i did not understand the last line of code could you explain it please??
|
|
|
|
|
string receiptString = lastReceiptNumber.ToString().PadLeft(receiptDisplayDigits, '0');
This line converts lastReceiptNumber to string, then adds as many 0 at its left as it's needed to have a string receiptDisplayDigits characters long.
You cas also do as musefan suggested and use receiptString = lastReceiptNumber.ToString("D" + receiptDisplayDigits); instead.
After this line, receiptString will be 00000249 .
string fullReceiptCode = string.Format("{0}-{1}", receiptCode, receiptString);
This line will concatenate the string created in the first line with receiptCode , using a template to define the string format.
You can also use a direct string concatenation (string fullReceiptCode = receiptCode + "-" + receiptString; ).
After this line, fullReceiptCode will be Rcp2008-00000249 .
|
|
|
|
|
Ahh ok ok man thanks alot i knew this but i forgot it because its been a while since i used {0} {1}...
|
|
|
|
|
It did not work the thing you wrote about padding left since when i convert to int
the 0000000000 will turn into 0
but thanks you gave me an answer about the padding thing
|
|
|
|
|
That's right, the padding is related to strings, if you convert back the string to int, you will lose al format you have applied to it.
|
|
|
|
|
I really like the logic you have used in getting a value with 8 digits, it's great to see people thinking along those lines rather than expecting things to be done with a simple 'out the box' line of code (and thus never really learning how things are actually acheived).
But in instances like this one-liners are the way to go, so hope this helps...
int newNumber = lastReceiptNumber + 1;
string displayNumber = newNumber.ToString("D" + receiptDisplayDigits);
Life goes very fast. Tomorrow, today is already yesterday.
|
|
|
|
|
Thanks man i really appreciate it, we ask question so we could learn how to solve them other wise we are not programers we are just people who ask questions
|
|
|
|
|
Exactly, and questions like yours will always be welcome in The Code Project forums
Life goes very fast. Tomorrow, today is already yesterday.
|
|
|
|
|
Man it worked great but could you explain what is the use with "D" i did not know what did it do.
|
|
|
|
|
some data types (such as int) have the option to overload the ToString method and provide a string containing format information. 'D' followed by a number ensures that the string contains as many preceding zeros as dictated by the number. have a look here[^] for more information
Life goes very fast. Tomorrow, today is already yesterday.
|
|
|
|
|
I am using Relection to get Methods from an Assembly.
I want to display the local Variable names in a method.
I have a test method called Test.
public void Test()
{
string myString = string.Empty;
int myInt = 0;
}
So I am using the following code:
MethodInfo[] methodInfo = type.GetMethods(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);
List<MethodInfo> methods= new List<MethodInfo>(methodInfo);
foreach (MethodInfo method in methods)
{
MethodBody methodBody = method.GetMethodBody();
IList<LocalVariableInfo> localVariables = methodBody.LocalVariables;
}
My problem is, even if I loop through the localVariables collection, I am not able to get the name of the variables of Test Method.
Kindly let me know how do I get the variable names.
Thanks in Advance.
|
|
|
|
|
Hi,
AFAIK local variables are not available in the meta data, except for the method's parameters.
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
Correct. Reflection only exposes the interface, not the implimentation.
Panic, Chaos, Destruction.
My work here is done.
|
|
|
|
|
Agreed, I know that,
but I could see the variable names in the method in ildasm so I thought there might be a way to get those. I know ildasm is DISASSEMBLER but took a chance.
Thanks for reply,
Aniruddha
|
|
|
|
|
Hi,
forget Reflector, it won't tell you about locals. You need either the source files or, maybe, the PDB file.
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|
|
Anir1982 wrote: Kindly let me know how do I get the variable names.
You can't and that is the end of it. It would require that reflection exposes the implimentation of the method rather than the interface.
What use would it be if you could in the long run? You cannot [or should not] be able to change the state of internal variables. Local variables only have scope within the life of a single call to the method. How or when would you be able to constructively use the instance or local variables of a class or method?
Panic, Chaos, Destruction.
My work here is done.
|
|
|
|
|
How Can I Select any Row In DataGridVeiw
|
|
|
|
|
example :
dataGridView1.Rows[3].Selected = true;
|
|
|
|
|
Thanks But How Can I MOve The ScrollBar To That Row
|
|
|
|
|
dataGridView1.FirstDisplayedScrollingRowIndex = 3; // set the index of row here..
|
|
|
|
|
I Am Greatfull.
Thanks Again
|
|
|
|
|
Hello!
I have a very simple question. If I want to create an event to my class I can use the EventHandler delegate like this:
public event EventHandler OnLog;
But the EventHandler delegate takes two arguments (the sender and an EventArgs object). If I don't want to have those arguments in my event, is there any other event handler delegate that takes zero arguments or so, or do I have to create the delegate myself?
Regards
|
|
|
|
|
Just use:
if(OnLog != null)
{
OnLog(null, EventArgs.Empty);
}
|
|
|
|
|
or define your own delegate.
or use an Action<type> so you can have exactly one argument with a type you choose.
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get.
Show formatted code inside PRE tags, and give clear symptoms when describing a problem.
|
|
|
|