1

Evening All,

I am extracting data from a API which exposes records as properties of the class so an example would be

For Each entry As Webservice.Names In objResponse.results
    string = string & "'firstname'=" & entry.firstname
    string = string & "'surname'=" & entry.surname
next

now we have about 50 entities in the webservice, and each has at least 10 "fields" my question is can I expose properties any other way ?

i.e. could i

For Each entry As Webservice.Names In objResponse.results
    for each prop in entry.properties
         string = string & "'" & prop.name & "'=" & entry.fields(prop.name)
    next
next

or similar, i can't seem to expose the properties above, but really looking for a way that i don't have to hand type every property against the string for inserting to the database.

any thoughts on how to achieve this within VB if the properties aren't exposed as above example

Thanks In Advance

2
  • why cant you insert to the db using entry.Foo or entry.fields(prop.name)? What value is added by moving them elsewhere if it is just for a db operation? Commented Apr 18, 2016 at 18:35
  • Thanks for the response, I can use Entry.foo, but this requires coding each property, i have a list of all properties and there Datatype for other purposes so was trying to something slightly more dynamic, that if the webservice extended (as it sometimes does), I don't have to recode to pick up the change. Commented Apr 18, 2016 at 22:08

1 Answer 1

1

You can get list of properties and their values using Reflection.

For Each entry As Webservice.Names In objResponse.results  
    for each prop in entry.GetType().GetProperties()
 ' use System.Reflection.BindingFlags to filter it.   
         string = string & "'" & prop.name & "'=" & prop.GetValue(entry,nothing) 
 ' use entry as first argument. it requires an instance of object to get 'property value  
   next  
next  
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks I'm gonna give this a go and let you know how i get on
@NewbieFromHell OK.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.