Mondeo Posted June 4, 2007 Posted June 4, 2007 (edited) MrPaul kindly helped me a while ago with the following code 'Somewhere in your header section: Imports System.Reflection 'Assume [i]myVehicle[/i] is the instance of Vehicle which you wish to alter and 'that [i]propertyName[/i] and [i]propertyValue[/i] contain the property name and value Dim vehType As Type vehType = myVehicle.GetType() 'Get the type of myVehicle 'Now, invoke a member to set the property vehType.InvokeMember(propertyName, _ BindingFlags.Public Or BindingFlags.Instance Or _ BindingFlags.SetProperty Or BindingFlags.FlattenHierarchy, _ Nothing, _ myVehicle, _ New Object() {propertyValue} _ ) This worked fine for the previous problem, now want to use it on a different project, but struggling! Basically I have 5 instances of a custom class as follows veh2yr10non = New MatrixVehicle(matrixID, "10000", "0", "2") veh2yr20non = New MatrixVehicle(matrixID, "20000", "0", "2") veh2yr30non = New MatrixVehicle(matrixID, "30000", "0", "2") veh2yr40non = New MatrixVehicle(matrixID, "40000", "0", "2") veh2yr50non = New MatrixVehicle(matrixID, "50000", "0", "2") I want to dynamically check the value of a property within the matrixvehicle classes. Ideally I need a method that can do this. Private Function GetClassProperty(byval classinstance as string, byval propertyinclass as string) as string End Function Dim s as string = GetClassProperty("veh2yr20non","chprice") Is this possible? Thanks Edited June 4, 2007 by PlausiblyDamp Quote
MrPaul Posted June 4, 2007 Posted June 4, 2007 Use a Dictionary or Hashtable As with your previous problem, setting a property based on the property name is very straightforward. The real problem here is that you wish to access a variable by its name. I suggest the simplest approach to this would be to use a Dictionary (or Hashtable in .Net 1.x) which associates each MatrixVehicle with its name. For example: 'Imports declaration Imports System.Collections.Generic 'At class level Private _mvecs As New Dictionary(Of String, Of MatrixVehicle) 'When creating MatrixVehicles _mvecs("veh2yr10non") = New MatrixVehicle(matrixID, "10000", "0", "2") _mvecs("veh2yr20non") = New MatrixVehicle(matrixID, "20000", "0", "2") _mvecs("veh2yr30non") = New MatrixVehicle(matrixID, "30000", "0", "2") _mvecs("veh2yr40non") = New MatrixVehicle(matrixID, "40000", "0", "2") _mvecs("veh2yr50non") = New MatrixVehicle(matrixID, "50000", "0", "2") 'Finally, GetClassProperty method Private Function GetClassProperty(ByVal vecInstance As String, ByVal propertyName As Atring) As String 'Get the vehicle Dim vec As MatrixVehicle = _mvecs(vecInstance) 'Get the vehicle type Dim vehType As Type vehType = vec.GetType() 'Now, invoke a member to get the property Dim retVal As Object retVal = vehType.InvokeMember(propertyName, _ BindingFlags.Public Or BindingFlags.Instance Or BindingFlags.GetProperty, _ Nothing, _ vec, _ New Object() {} ) 'Return as String If (retVal IsNot Nothing) Then Return retVal.ToString() Else Return "Null" End Function It might take a little tweaking but hopefully you get the idea. If you wanted to be able to use the method to get the property of any type of object (not just MatrixVehicle) then you could use a Hashtable or a more general Dictionary and it should still work. Good luck :cool: Quote Never trouble another for what you can do for yourself.
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.