Click here to Skip to main content
15,868,002 members

The Weird and The Wonderful

   

The Weird and The Wonderful forum is a place to post Coding Horrors, Worst Practices, and the occasional flash of brilliance.

We all come across code that simply boggles the mind. Lazy kludges, embarrassing mistakes, horrid workarounds and developers just not quite getting it. And then somedays we come across - or write - the truly sublime.

Post your Best, your worst, and your most interesting. But please - no programming questions . This forum is purely for amusement and discussions on code snippets. All actual programming questions will be removed.

 
GeneralRe: Taking responsibility Pin
jschell8-May-22 9:08
jschell8-May-22 9:08 
GeneralPathYetAnotherMakeUniqueName Pin
Brisingr Aerowing4-Apr-22 7:56
professionalBrisingr Aerowing4-Apr-22 7:56 
GeneralRe: PathYetAnotherMakeUniqueName Pin
raddevus4-Apr-22 10:14
mvaraddevus4-Apr-22 10:14 
GeneralRe: PathYetAnotherMakeUniqueName Pin
Mircea Neacsu4-Apr-22 13:20
Mircea Neacsu4-Apr-22 13:20 
GeneralRe: PathYetAnotherMakeUniqueName Pin
Cpichols6-Apr-22 1:21
Cpichols6-Apr-22 1:21 
GeneralRe: PathYetAnotherMakeUniqueName Pin
deepok14-Apr-22 19:35
deepok14-Apr-22 19:35 
GeneralRe: PathYetAnotherMakeUniqueName Pin
ShawnVN6-Apr-22 7:16
ShawnVN6-Apr-22 7:16 
GeneralNull Strikes Again! (even with C# special operators) Pin
raddevus22-Feb-22 11:25
mvaraddevus22-Feb-22 11:25 
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.

C#
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:

C#
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:

C#
private Object convertDbNull(String? s){
  if (s == null){
   return System.DBNull.Value;
  }
  return s;
}


Then call my code like this:

C#
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. Laugh | :laugh:

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). Smile | :)

modified 22-Feb-22 17:39pm.

GeneralRe: Null Strikes Again! (even with C# special operators) Pin
Jon McKee22-Feb-22 11:44
professionalJon McKee22-Feb-22 11:44 
GeneralRe: Null Strikes Again! (even with C# special operators) Pin
raddevus22-Feb-22 11:50
mvaraddevus22-Feb-22 11:50 
GeneralRe: Null Strikes Again! (even with C# special operators) Pin
raddevus22-Feb-22 11:53
mvaraddevus22-Feb-22 11:53 
GeneralRe: Null Strikes Again! (even with C# special operators) Pin
Paulo Zemek22-Feb-22 20:24
mvaPaulo Zemek22-Feb-22 20:24 
GeneralRe: Null Strikes Again! (even with C# special operators) Pin
Super Lloyd10-Mar-22 12:48
Super Lloyd10-Mar-22 12:48 
GeneralRe: Null Strikes Again! (even with C# special operators) Pin
jschell8-May-22 9:17
jschell8-May-22 9:17 
GeneralBuT cAn ThEY RuN DoOM? Pin
honey the codewitch7-Jan-22 6:14
mvahoney the codewitch7-Jan-22 6:14 
GeneralRe: BuT cAn ThEY RuN DoOM? Pin
jeron17-Jan-22 6:25
jeron17-Jan-22 6:25 
GeneralRe: BuT cAn ThEY RuN DoOM? Pin
honey the codewitch7-Jan-22 6:33
mvahoney the codewitch7-Jan-22 6:33 
GeneralRe: BuT cAn ThEY RuN DoOM? Pin
jeron17-Jan-22 6:43
jeron17-Jan-22 6:43 
GeneralRe: BuT cAn ThEY RuN DoOM? Pin
deepok14-Apr-22 5:44
deepok14-Apr-22 5:44 
GeneralRe: BuT cAn ThEY RuN DoOM? Pin
jeron14-Apr-22 5:53
jeron14-Apr-22 5:53 
GeneralRe: BuT cAn ThEY RuN DoOM? Pin
deepok14-Apr-22 5:58
deepok14-Apr-22 5:58 
GeneralRe: BuT cAn ThEY RuN DoOM? Pin
Alister Morton6-Apr-22 3:59
Alister Morton6-Apr-22 3:59 
GeneralRe: BuT cAn ThEY RuN DoOM? Pin
Super Lloyd7-Jan-22 11:56
Super Lloyd7-Jan-22 11:56 
GeneralRe: BuT cAn ThEY RuN DoOM? Pin
honey the codewitch7-Jan-22 12:17
mvahoney the codewitch7-Jan-22 12:17 
GeneralRe: BuT cAn ThEY RuN DoOM? Pin
Super Lloyd7-Jan-22 12:05
Super Lloyd7-Jan-22 12:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.