|
Ooh! A punster! Very nice
|
|
|
|
|
Most probably a variant to template. It will pretty neat in a medieval game dev scenario :)
|
|
|
|
|
History:
This was part of a batch of embarrassing internal junk, that lawyers forced to document as part of the DoJ consent decree in 2001. Basically, various API surface in Windows (mostly helper functions in Shell32.dll) that other products (mostly Office) were calling.
It was never intended to see light of day.. obviously, it was the result of some dev who rotated from one org to another, and copy-pasted some helper function code from one product repo to another.
I don't recall any of those lawyer-documented functions doing anything meaningful (that couldn't be replicated in user-mode code using existing Win32 APIs). But, try explaining that to DoJ lawyers..
|
|
|
|
|
I believe you'll find this interesting and a possible discussion.
But, also, I ain't too smart so I may just not be seeing something.
I'm using the cutting edge latest version of C# 10 (in .NET Core 6.0.2 web api I'm building).
I'm saving data to a sqlite database & I have some code where I want to allow the value to be null (inserted into the db) if hte user doesn't supply that data.
sqliteProvider.command.Parameters.AddWithValue("$screenName", task.screenName);
task.screenName is a nullable string.
Null This, Null That -- There are different kinds of null
However, if the value is actually a null string because the user has opted to not provide the value then I need to actually insert a System.DbNull.Value into the databse -- that's a true DBNull -- not the String null which the db chokes on.
The New Null Operators
So, I'm new & I'm hip to the scene of these null coalescing (??= ??), Elvis operators (?: ) and all that stuff, you dig man?
But it's a no-go.
Null Still Ate My Code
Here's what I wanted.
I wanted to call the AddWithValue() with the String value when the nullable string (task.screenName) isn't null...
....and I wanted to call AddWithValue() with a System.DbNull.Value (so null would be properly passed and inserted into the db) when the nullable String is null.
I was hoping for something like:
sqliteProvider.command.Parameters.AddWithValue("$screenName", task.screenName ?? System.DbNull.Value);
I was hoping it would say, "if the String is null, then use the System.DBNull.Value.
Two Different Types
But, like, far out and way out man. It ain't happening, because those are two different types.
And, dig it, to get these null operators to work, they have to be the same type.
So I had to write a method:
private Object convertDbNull(String? s){
if (s == null){
return System.DBNull.Value;
}
return s;
}
Then call my code like this:
sqliteProvider.command.Parameters.AddWithValue("$screenName",convertDbNull(task.ScreenName));
It'll return the valid String (when not null) or it'll return the System.DBNull.Value (when the string is null).
Surely, I'm Missing Some Super Special Null Operator??
If you have a better way where I can use one of the new null operators, please, please lay it on me.
So the Multi-Billion-dollar Null Problem still exists, apparently. (https://www.infoq.com/presentations/Null-References-The-Billion-Dollar-Mistake-Tony-Hoare/[^])
I eagerly await your input on this (seriously).
modified 22-Feb-22 17:39pm.
|
|
|
|
|
sqliteProvider.command.Parameters.AddWithValue("$screenName", (object) task.screenName ?? System.DbNull.Value);
That should work. C# won't coerce a type (even if it's valid) to a type not present in the expression. Here it sees string and DBNull , so even though object is a valid base for them, object isn't available unless you explicitly opt-in. That's my understanding at least
EDIT: You might need a ? for the cast. Unsure since I'm still on an older version of C# so I haven't had to deal with that new stuff yet.
|
|
|
|
|
Very good point and thanks for posting.
I will try this out right now and let you know.
|
|
|
|
|
Oh my gosh!! I was so close!!!
That works -- well it compiles anyways. I will test to insure that it definitely does what I expect, but it looks good:
sqliteProvider.command.Parameters.AddWithValue("$screenName",(object)task.ScreenName ?? System.DBNull.Value);
Wow!
confirmed!! I built it and ran it and it works.
I'm so glad I ranted.
Thanks again.
|
|
|
|
|
That's an API problem, not really related to null or not null, but the fact that .NET DB drivers assume "null" as unset, and DBNull.Value as real null (WPF did it a little better, as we have Unset as a separate item, and null really means null).
You can probably create a helper/extension method that converts null to DBNull.Value and it should work fine.
I also consider it very bad that IDataParameter always box value types. It shouldn't need to do it, as I can clearly tell by working on C++ drivers that primitive types can just be primitive types and never "boxed" or similar there.
|
|
|
|
|
|
raddevus wrote: I need to actually insert a System.DbNull.Value into the databse -- that's a true DBNull -- not the String null which the db chokes on.
That statement makes no sense.
Given a string/text value in a database it can have one of the following values.
1. Null
2. Empty string. Some databases do not allow this
3. Any other non-empty value.
That list does not include 'DBNull'.
The 'DBNull' value is usually a value that is intended as a stand in for results for the first item in the list above.
Perhaps your real problem is that you need to represent the following
1. A value that is not null
2. User did not 'choose' anything. So you need to represent 'no value', but you cannot use 'null'.
In the above you then use a magical value. If your database supports it and there NO chance it will be needed then an empty string can be used. But otherwise some nonsensical value is chosen.
For example say you need to provide a customer telephone number. So you allow numbers. But for your special value you allow 'xxx-xxx-xxxx'. Naturally you must special case the code.
HOWEVER, this is not an idea solution. And you should first examine the requirements that state that 'null' cannot be used in the first place. That the database does not allow it is NOT a argument for that. Because that to is just code and it must meet the needs of the business and not arbitrary developer opinions.
|
|
|
|
|
|
First driving goldfish, now rats playing doom?
Should we be getting ready to bow to our pea-sized brain overlords?
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment
"Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst
"I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
|
|
|
|
|
Well to be fair, the size of brain doesn't seem to matter much. Rats are probably smarter than cats, and one of the few animals capable of metacognition (thinking about thinking basically - little philosophers, they are!)
One of the reasons I'm such a fan of scavenging animals like rats, racoons and crows is they are bloody intelligent. It makes them interesting as animals go.
Real programmers use butterflies
|
|
|
|
|
honey the codewitch wrote: little philosophers, they are! SocRATes!
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment
"Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst
"I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
|
|
|
|
|
Well, there is a video in which an orangutan driving a golf cart and even take the turns
|
|
|
|
|
I pretty certain they were in front of me this morning.
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment
"Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst
"I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
|
|
|
|
|
|
A couple of years ago we had a TV show where they took a bunch of different pet dogs and whittled them down via various agility and obedience tests etc until they had a dog which they taught to pilot an light aircraft, by using lights to indicate to it what adjustments to make. It didn't just have to fly straight and level, though, they set it a route it had to navigate.
They didn't expect it to take off and land, of course. A pilot took it up, then handed the controls over to the dog.
|
|
|
|
|
Wrong section, you should have posted that in the newsletter!
And I am ever so happy I navigated on this forum out of boredom!
|
|
|
|
|
I thought this qualified as weird at least. =)
Real programmers use butterflies
|
|
|
|
|
Mm quite the Rube Goldberg's contraption. Though I have to say, the rat doesn't seem very invested in the game to me!
|
|
|
|
|
|
OT: I've seen the randomly capitalized headlines for a while now. Is there a rule for 'randomizing' the capitals? Just trying to make certain I'm up to date on all the important stuff...
Rats playing DOOM seems kinda pointless. After all, it can be played on toasters. Teach them to perform open-heart surgery, though, and I'll be impressed!
|
|
|
|
|
David O'Neil wrote: OT: I've seen the randomly capitalized headlines for a while now. Is there a rule for 'randomizing' the capitals? Just trying to make certain I'm up to date on all the important stuff...
Sometimes it can be used to hold a secret message (e.g. just read the uppercase letters or just the lowercase letters), but that doesn't reveal anything obvious in this instance. 'RuN DoOM' is almost RANDOM; perhaps it should have been 'RAts ruN DoOM'
The longest alternating consonant/vowel/consonant/vowel country name (like cAnAdA / cUbA) is UnItEd ArAb EmIrAtEs.
|
|
|
|
|