iOS Question check if a kvs custom type contains an element

marcick

Well-Known Member
Licensed User
Longtime User
I have a KeyValueStore that contains custom type

B4X:
Type LocCfg(Password As String, Confirmed As Boolean, Cdate As Long)

Now I need to add a field and I modify like this

B4X:
Type LocCfg(Password As String, Confirmed As Boolean, Cdate As Long, NewField as int)

I publish on the store the new app

First time the new app is run, the existing kvs does not have the NewField, so I'm trying to do like this

B4X:
Dim par As LocCfg=KvsLocCfg.Get(key1)
if par.NewField=null or par.NewField="" then par.NewField=0
dim x as int=par.NewField

But it generate a "null" error

Also adding a Try-Catch does not work

B4X:
Dim par As LocCfg=KvsLocCfg.Get(key1)
Dim x as int
try
    x=par.NewField
catch
   x=0
End Try

Which is the correct way to manage this ?
 

kstainsb

Member
Licensed User
Longtime User
Could you define a new data type LocCfg2 with the new data elements. Then get the object from kvs, and do a check. If obj is LocCfg then...else if obj is LocCfg2 ... etc?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Could you define a new data type LocCfg2 with the new data elements. Then get the object from kvs, and do a check. If obj is LocCfg then...else if obj is LocCfg2 ... etc?
This is a good option. You can also add LocCfg as a field in LocCfg2. This way you will only need to add the new field.

But it generate a "null" error
Can you post the full error message?
 
Upvote 0

marcick

Well-Known Member
Licensed User
Longtime User
Dim par As LocCfg=KvsLocCfg.Get(key1)
if par.NewField=null or par.NewField="" then par.NewField=0
dim x as int=par.NewField

The error in the last instruction is: "Cannot parse: (null)"

I feel there is a simpler way to solve, but I did like this and seems to work:

B4X:
Dim parold As LocCfg
Dim parnew As LocCfg2
Dim xx As Object
For Each k As String In MySub.KvsLocCfgWeb.ListKeys
      xx=MySub.KvsLocCfgWeb.get(k)
      If xx Is LocCfgWeb Then
           parold=MySub.KvsLocCfgWeb.Get(k)
           parnew.Password=parold.Password
           parnew.Cdate=parold.Cdate
           parnew.NewField=0
           MySub.KvsLocCfgWeb.put(k,parnew)
      End If
Next
 
Upvote 0
Top