Android Question Testing the type of a variable

Troberg

Well-Known Member
Licensed User
Once again, I'm pondering my settings framework. I still don't like to have:

B4X:
* SettingStr(Key as string, Value as string, Write as bool) as string
* SettingInt(Key as string, Value as int, Write as bool) as int
* SettingLong(Key as string, Value as long, Write as bool) as long
* SettingBool(Key as string, Value as bool, Write as bool) as bool
* SettingFloat(Key as string, Value as float, Write as bool) as float
* SettingDouble(Key as string, Value as double, Write as bool) as double
* SettingDate(Key as string, Value as long, Write as bool) as long

Yet, I do want to do some special stuff on some types, to maintain human readability. This mainly applies to bools (save as Yes/No) and dates (save in ISO format).

As overloading is not likely to happen soon, I started thinking about doing this:

B4X:
* Setting(Key as string, Value as object, Write as bool) as object

Then I'd test the type of the Value argument and do whatever formatting needed. Possible?
 

DonManfred

Expert
Licensed User
Sadly B4a does not allow overloading
 
Upvote 0

eurojam

Well-Known Member
Licensed User
did you try:
B4X:
    Dim o As Object
    o = Setting("abc","abc",True)
    Dim VarType As String = GetType(o)
    Log(VarType)

and then parse the return string like:
  • java.lang.String
  • java.lang.Double
....and so on
 
Upvote 0
Top