Acuario Active Member Licensed User Longtime User Dec 6, 2022 #1 I'm reading values from the KeyValueStore. When the value returns null it seems impossible to test for in a way that seems intuitive. i.e. the following does not work and the value continues to be null: Code: Copy Dim deviceID as String Dim mykvs As KeyValueStore mykvs.Initialize(File.DirApp, "settings") deviceID = mykvs.Get("device") If deviceID = Null Then deviceID = "test" If deviceID = "" Then deviceID = "test" In both cases deviceID remains as null I know it can be solved in the following way: B4X: Copy Dim o As Object mykvs.Initialize(File.DirApp, "settings") o = mykvs.Get("device") If o = Null Then deviceID = "test" Else deviceID = o End If ..but this seems long and just 'feels' wrong.. Is there an alternative way? Maybe a solution would be a method that passes a default value that is returned if the kvs value was null?
I'm reading values from the KeyValueStore. When the value returns null it seems impossible to test for in a way that seems intuitive. i.e. the following does not work and the value continues to be null: Code: Copy Dim deviceID as String Dim mykvs As KeyValueStore mykvs.Initialize(File.DirApp, "settings") deviceID = mykvs.Get("device") If deviceID = Null Then deviceID = "test" If deviceID = "" Then deviceID = "test" In both cases deviceID remains as null I know it can be solved in the following way: B4X: Copy Dim o As Object mykvs.Initialize(File.DirApp, "settings") o = mykvs.Get("device") If o = Null Then deviceID = "test" Else deviceID = o End If ..but this seems long and just 'feels' wrong.. Is there an alternative way? Maybe a solution would be a method that passes a default value that is returned if the kvs value was null?
Magma Expert Licensed User Longtime User Dec 6, 2022 #2 @Acuario I prefer checking deviceid.As(string).Trim.Length<1 Upvote 0
stevel05 Expert Licensed User Longtime User Dec 6, 2022 #3 It should only return Null if you haven't assigned a value to that key (assuming you do not assign Null for some reason), otherwise it will be whatever value you assigned to it. B4X: Copy Dim DeviceID As String If mykvs.ContainsKey("device") Then DeviceID = mykvs.Get("device") Else 'Not assigned DeviceID = "test" End If Should do it Last edited: Dec 6, 2022 Upvote 0
It should only return Null if you haven't assigned a value to that key (assuming you do not assign Null for some reason), otherwise it will be whatever value you assigned to it. B4X: Copy Dim DeviceID As String If mykvs.ContainsKey("device") Then DeviceID = mykvs.Get("device") Else 'Not assigned DeviceID = "test" End If Should do it
agraham Expert Licensed User Longtime User Dec 6, 2022 #4 This works because comparisons take the type of the left most value so the right side value is converted to the type of the left, which here is a string. B4X: Copy If deviceID = "null" Then deviceID = "test" End If Interestingly the compiler does this at compile time rather than trying to cast Null to a string at runtime, so this must be deliberate behaviour on Erel's part as it needs compile time checks on the comparison type to establish what to do with the Null value if ((mostCurrent._deviceid).equals("null")) { You can't use Null as the left value because it causes a null object exception. Upvote 0
This works because comparisons take the type of the left most value so the right side value is converted to the type of the left, which here is a string. B4X: Copy If deviceID = "null" Then deviceID = "test" End If Interestingly the compiler does this at compile time rather than trying to cast Null to a string at runtime, so this must be deliberate behaviour on Erel's part as it needs compile time checks on the comparison type to establish what to do with the Null value if ((mostCurrent._deviceid).equals("null")) { You can't use Null as the left value because it causes a null object exception.
Acuario Active Member Licensed User Longtime User Dec 6, 2022 #5 stevel05 said: It should only return Null if you haven't assigned a value to that key (assuming you do not assign Null for some reason), otherwise it will be whatever value you assigned to it. B4X: Copy Dim DeviceID As String If mykvs.ContainsKey("device") Then DeviceID = mykvs.Get("device") Else 'Not assigned DeviceID = "test" End If Should do it Click to expand... Ok, thanks to all - handling null values is, in many languages, never what it seems. I think @stevel05 has a slightly improved way than mine by checking for existence of the key before trying to assign it - obviously the first instance of reading a key in many cases would be a null (usernames/passwords - user data in general comes to mind). I guess I come from the Windows GetPrivateProfileInt/String etc. where you can specify a default return value in the case of the entry not existing. Oh well, we live/code with what we have and make the most of it.... Upvote 0
stevel05 said: It should only return Null if you haven't assigned a value to that key (assuming you do not assign Null for some reason), otherwise it will be whatever value you assigned to it. B4X: Copy Dim DeviceID As String If mykvs.ContainsKey("device") Then DeviceID = mykvs.Get("device") Else 'Not assigned DeviceID = "test" End If Should do it Click to expand... Ok, thanks to all - handling null values is, in many languages, never what it seems. I think @stevel05 has a slightly improved way than mine by checking for existence of the key before trying to assign it - obviously the first instance of reading a key in many cases would be a null (usernames/passwords - user data in general comes to mind). I guess I come from the Windows GetPrivateProfileInt/String etc. where you can specify a default return value in the case of the entry not existing. Oh well, we live/code with what we have and make the most of it....
Magma Expert Licensed User Longtime User Dec 6, 2022 #6 by the way... I want to ask you Acuario... is that a "mask" at your thumb-image-photo or a "glass-of-red-wine" ? Upvote 0
by the way... I want to ask you Acuario... is that a "mask" at your thumb-image-photo or a "glass-of-red-wine" ?