7
u/hillman_avenger Sep 06 '16
Why is there even a Convert.ToBoolean(boolean) function? To help spot shittyprogrammers?
12
u/ab7a71 Sep 06 '16
Hello, That isnt the case, its actually Convert.ToBoolean(object) and all .Net objects are based off of this. :)
4
u/frisch85 Sep 06 '16 edited Sep 06 '16
if(String.IsNullOrEmpty(txtCostPerHour.Text) || String.IsNullOrEmpty(tstHoursPerDay.Text)) {
}1
u/ab7a71 Sep 06 '16
I actually like this. But prefer IsNullOrWhiteSpace(). Can I ask why you didnt use this instead?
1
u/frisch85 Sep 06 '16
I edited my post sorry for misleading to .IsEmpty. IsNullOrWhiteSpace makes more sense i guess when used on form properties. We used IsNullOrEmpty in our software to also avoid nullreference-Exceptions but i guess that's covered by IsNullOrWhiteSpace, too.
I am not up-to-date regarding the framework progress but we started on framework version 2.0 which didn't have the IsNullOrWhiteSpace method.
1
1
u/iinlane Sep 06 '16
There is no string.IsEmpty() function.
2
u/frisch85 Sep 06 '16
Dang it, sorry it's been couple of years since i've written my last C# program... i'll correct my post immediately.
6
u/iinlane Sep 06 '16
:)
It's weird to write code without autocomplete. I once had to write code on paper and failed miserably - no autocomplete, no backspace, no line insertions, no google or help. Wouldn't recommend.
2
u/frisch85 Sep 06 '16
Yeah lol, when working on my userscripts in tampermonkey i often have to lookup the exact names of some methods... It can get really confusing because in javascript public mehtods are written in camel case while in C# they are written in pascal case...
1
u/Jabbersii Sep 06 '16
Just to nitpick, string.IsNullOrWhiteSpace() will also do an empty check: https://msdn.microsoft.com/en-us/library/system.string.isnullorwhitespace(v=vs.110).aspx
1
u/iinlane Sep 06 '16
It was introduced in .NET 4. Chances are the old program is in .net 2 or 3.5. In this application I would prefer decimal.TryPrase(txthours.Text, out hours).
2
u/Prod_Is_For_Testing Sep 07 '16
Using out variables just for an if condition is annoying if you can find another option (since the variable has to be declared in a parent scope). This should be remedied in C# 7 because they're adding out variable declarations within the function call itself
2
1
1
1
1
u/ekolis Sep 10 '16
Check the timestamp on the source file; he wants a boxed Boolean rather than an unboxed bool. Must be December 26.
0
u/tophatstuff Sep 06 '16
Eh, I can see why you might get into the habit of using a fancy ToBoolean function if you used a heathen language where strings and integers and pointers, empty/0/null or otherwise, aren't truthy/falsey without a cast.
-3
u/dadibom Sep 06 '16
Meh. if (!txtThingy1.Text || !txtThingy2.Text)
3
u/aerno Sep 06 '16
also shitty
2
1
u/dadibom Sep 06 '16
Care to elaborate? I've never used typescript, if it's because of that. I find it to be very useful, given that you actually know what's happening, because I usually don't care if it's null/undefined/0/false/whatevs, since these ifs are usually for "if the string is empty".
2
1
5
u/nictytan Sep 06 '16
Use de Morgan's law and have one less operation!
!(string1 && string2)But yeah in a strongly typed language like C# (OP's example) this won't work of course.
0
Sep 06 '16
[deleted]
9
u/ab7a71 Sep 06 '16
Hey, what would a compiler do in this case?
1
0
Sep 06 '16
[deleted]
19
u/Cathercy Sep 06 '16
Not familiar with this Convert object or even what language this is, but this seems a bit more complex. Not sure that a compiler would catch it.
2
12
2
1
0
Sep 06 '16
[deleted]
2
u/nictytan Sep 06 '16
There's nonetheless the overhead of a static method lookup, and a function call (extremely expensive compared to simply reusing the bool.)
1
25
17
u/nemec Sep 06 '16 edited Sep 06 '16
Maybe after JIT, but it doesn't seem to be compiled away:
if (Convert.ToBoolean(true)) { Console.WriteLine("hello"); } IL_0000: nop IL_0001: ldc.i4.1 IL_0002: call System.Convert.ToBoolean IL_0007: stloc.0 IL_0008: ldloc.0 IL_0009: brfalse.s IL_0018 IL_000B: nop IL_000C: ldstr "hello" IL_0011: call System.Console.WriteLine IL_0016: nop IL_0017: nop IL_0018: retEdit: I should note that this is the output from LINQPad 5, which uses Roslyn to compile.
13
u/AlGoreBestGore Sep 06 '16
Can't trust those double equals.