function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

dynamically reference Object fields VB.NET


Does anyone know how to dynamically reference the field names in an Object?

I'm populating a dataset from a queryresult. The list of fields and the object it queries is built dynamically so explicity identying each field is not feasible.

For i As Integer = 0 To qResult.records.GetUpperBound(i)

con = CType(qResult.records(i), sforceDEV.Contact)

dsR("FirstName") = con.FirstName



The Fieldnames in the dataset I'm constructing from a picklist, but for the example here, I've changed it to read:

 dsR("FirstName") for simplicity reasons. The area I need to populate is for each field name in "con".





You can use reflection to access the object, or use the Partner WSDL, which is not strongly typed and makes it easier to do this sort of work. Take a look at the sforce explorer sample which creates datasets from query results. Also you might want to look at the sforce managed data provider, which gives you an ADO.NET provider layered ontop of the sforce API. both are available from

How do I use this "reflection" Can you provide an example?

I've looked at the various examples, including the sforce browser, which do not seem to provide VB.Net examples.



I recommend using the Partner WSDL. It makes life much easier if you're planning on working with the sforce objects dynamically.

That said, here's a link to a bit of info on using reflection in VB.NET. I don't vouch for this, but it's a good starting point, along with Google.

import System.Reflection

dim conType as Type

conType = con.GetType()

once you have a handle on the object's type, you have access to all of it's properties, methods, etc...


dim conProps as PropertyInfo()

dim conProp as PropertyInfo

conProps = conType.GetProperties()  'returns a collection of PropertyInfo objects that you can invoke

foreach conProp in conProps


next conProp


sorry - forgot to finish my loop

foreach conProp in conProps

'name = conProp.Name

'value = conProp.GetGetMethod().Invoke(con, ...)

next conProp


sorry for my shaky vb, but you should get the idea.  If going this route, be sure to cache all the property info, it can be quite expensive to recreate over and over...