You need to sign in to do that
Don't have an account?
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
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.
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
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
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...