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
OTOT 

dynamically reference Object fields VB.NET

Hi,

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

dsT.Rows.Add(dsR)

Next

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".

 

Thanks,

RJ

 

SuperfellSuperfell
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 http://sourceforge.net/projects/sforce/
OTOT

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.

 

 

HaroldHHaroldH
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.

http://visualbasic.about.com/od/usingvbnet/a/reflection1_1X.htm
ScottGScottG

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...

i.e.

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

string

next conProp

ScottGScottG

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...