T Troberg Well-Known Member Licensed User Longtime User Apr 22, 2015 #1 I want to get a property of an object of (semi)unknown type. By that, I meant that I don't know exactly what type the object is, but I know that it has the property. I've tried this, but it fails (java.lang.RuntimeException: java.lang.NoSuchFieldException: MenuPresence): Code: Copy Sub GetProperty(obj As Object, Property As String) As Object Dim r As Reflector r.Target = obj Return r.GetPublicField(Property) End Sub Of course, I can do it like this: Code: Copy Sub GetProperty(obj AsObject, Property AsString) AsObject ReturnCallSub(obj, "get" & Property) End Sub However, that assumes that all properties have getters and setters, and are not simply public variables of the class. What am I doing wrong here???
I want to get a property of an object of (semi)unknown type. By that, I meant that I don't know exactly what type the object is, but I know that it has the property. I've tried this, but it fails (java.lang.RuntimeException: java.lang.NoSuchFieldException: MenuPresence): Code: Copy Sub GetProperty(obj As Object, Property As String) As Object Dim r As Reflector r.Target = obj Return r.GetPublicField(Property) End Sub Of course, I can do it like this: Code: Copy Sub GetProperty(obj AsObject, Property AsString) AsObject ReturnCallSub(obj, "get" & Property) End Sub However, that assumes that all properties have getters and setters, and are not simply public variables of the class. What am I doing wrong here???
Erel B4X founder Staff member Licensed User Longtime User Apr 22, 2015 #2 The actual field name is _<lower cased field name>. Unless the code is obfuscated. In that case the field name is random. Upvote 0
The actual field name is _<lower cased field name>. Unless the code is obfuscated. In that case the field name is random.
LucaMs Expert Licensed User Longtime User Apr 22, 2015 #3 Troberg said: Of course, I can do it like this: Code: Copy Sub GetProperty(obj AsObject, Property AsString) AsObject ReturnCallSub(obj, "get" & Property) End Sub Click to expand... Off topic, I know, sorry. Is ReturnCallSub a new b4a keyword/function? Thanks Upvote 0
Troberg said: Of course, I can do it like this: Code: Copy Sub GetProperty(obj AsObject, Property AsString) AsObject ReturnCallSub(obj, "get" & Property) End Sub Click to expand... Off topic, I know, sorry. Is ReturnCallSub a new b4a keyword/function? Thanks
DonManfred Expert Licensed User Longtime User Apr 22, 2015 #4 I dont think so. Probably a sub From troberg... Upvote 0
T Troberg Well-Known Member Licensed User Longtime User Apr 23, 2015 #5 Erel said: The actual field name is _<lower cased field name>. Click to expand... Thanks, that would have taken me a lot of time to figure out. Is ReturnCallSub a new b4a keyword/function? Click to expand... Nope, much simpler than that. Sometimes when I copy-paste from the IDE to the web browser, some spaces are lost (for example, every space after "As"). Don't know why, but it happens. Upvote 0
Erel said: The actual field name is _<lower cased field name>. Click to expand... Thanks, that would have taken me a lot of time to figure out. Is ReturnCallSub a new b4a keyword/function? Click to expand... Nope, much simpler than that. Sometimes when I copy-paste from the IDE to the web browser, some spaces are lost (for example, every space after "As"). Don't know why, but it happens.
T Troberg Well-Known Member Licensed User Longtime User Apr 27, 2015 #6 Still don't work. I've given up on that approach now, and use CallSub on the getters and setters. Upvote 0