|
A padded number in a file name!
int fileCount = Directory.GetFiles("[censored]", "*.csv", SearchOption.AllDirectories).Length + 1;
string fileCountStr = (fileCount <= 999 ? fileCount <= 99 ? fileCount <= 9 ? "000" : "00" : "0" : "") + fileCount;
I think this should win an award for readability in a triply-nested ternary condition .
|
|
|
|
|
And what about when someone deletes 085.csv ?
speramus in juniperus
|
|
|
|
|
Oh, don't worry. None of the accounts have delete permissions. 
|
|
|
|
|
It's so good to know that the hard work of the developers of String.PadLeft[^] and Int32.ToString[^] is providing so much joy to so many, other than you.
By the way, what language is that? It can't be C#, or did you also override the + operator? 
|
|
|
|
|
PIEBALDconsult wrote: It can't be C#
Really?
When one or both operands are of type string, + concatenates the string representations of the operands.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Richard Deeming wrote: When one or both operands are of type string
Yes, and are they?
|
|
|
|
|
PIEBALDconsult wrote: Yes, and are they?
Yes, the first operand is a string. It's the result of the ternary operator:
fileCount <= 999
? fileCount <= 99
? fileCount <= 9
? "000"
: "00"
: "0"
: ""
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
|
PIEBALDconsult wrote: And the second?
Doesn't matter. The documentation clearly states, "When one or both operands are of type string...", so the type of the other operand is irrelevant.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I missed that the first time also.
BDF
The internet makes dumb people dumber and clever people cleverer.
-- PaulowniaK
|
|
|
|
|
|
|
PIEBALDconsult wrote: I'm fairly sure that no self-respecting C# professional uses it that way. Heh, I go for the shorter version. If it doesn't make sense with a mishmash of ints/strings I use brackets or ToString() to clarify.
int i = 124, j = 253;
string s = "JD" + i + j;
string t = "JD" + (i + j);
But yeah, usually I don't bother.
Although, theoretically, I could now do
string s = 0;
string t = "" + 0;
So, let's get back to self-respecting... 
|
|
|
|
|
string t = System.String.Format ( "JD{0}" , i + j ) ;
|
|
|
|
|
Make that:
string t = string.Format(CultureInfo.InvariantCulture, "JD{0:D}", i + j);
You typically don't want culture-specific formatting or group separators in the result.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I've never had that problem.
|
|
|
|
|
And of course don't forget to set the string to "" at the end, else if the garbage collector is a little slow you might run out of memory!
Better yet, just keep re-using global strings.
|
|
|
|
|
I think I wrote it in one of those points in the week where I just couldn't think. Methinks I'll replace it tomorrow.
|
|
|
|
|
Wonderful code!
When I was new to programming I used to do something like this too.
Then I figured out a trick: just add 1.000.000.000 to the number and take the last 4 characters.
Now I just use PadLeft, PadRight or Format
|
|
|
|
|
Just found this gem in the .NET BCL documentation on Process.WaitForExit(Int32):
In the .NET Framework version 3.5 and earlier versions, if milliseconds was -1, the WaitForExit(Int32) overload waited for MaxValue milliseconds (approximately 24 days), not indefinitely.
Regards,
mav
--
Black holes are the places where God divided by 0...
|
|
|
|
|
I think you will find that 24 days is the maximum number of milliseconds a int32 wil hold.
How do I know this?
Well I wrote a millisecond counter with an int32 and wondered why every 20 or so days the program which was running on someone else's machine crashed.
I never hit the error as I switched my machine off every night and was only away on holiday for less than 20 days when the software was running without a nightly shutdown.
[edit - I missed the first line...]
“That which can be asserted without evidence, can be dismissed without evidence.”
― Christopher Hitchens
|
|
|
|
|
The difference between eternity and 24 days matters actually. Windows 9x did crash after 49.7 days. They used unsigned int back then...
“Today is the first day of the rest of your life.”
|
|
|
|
|
I'm currently working with a GUI built by someone who seems to like using the default names for controls. The language in which this GUI was created allows any control to have a label associated with it. I have just come across a control named copyOfcopyOfcopyOfTextField41282 , with a label of copyOfcopyOfcopyOfTextField4121 .
In this language, it is also common to refer to a control by way of mentioning all its parent controls (which in the GUI builder is often placed in a dropdown with a fixed width to ensure that you cannot see the full control path and name, because that would just make your life too easy). The full path and name of the above control is Group0/Frame/CriteriaBox/VerticalBox2/copyOfcopyOfVerticalBox512/HorizontalBox5/copyOfcopyOfcopyOfTextField41282 .
The control in question isn't even visible to the user, and I have no idea what its purpose is.
Kill me now.
What is this talk of release? I do not release software. My software escapes leaving a bloody trail of designers and quality assurance people in its wake.
|
|
|
|
|
BotCar wrote: I have no idea what its purpose is
It's obvious. It's to make really, really, really sure that you don't lost the contents of TextField41282.
|
|
|
|
|
try deleting them and see if anybody notices 
|
|
|
|