Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (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 by PlausiblyDamp
Posted

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:

Never trouble another for what you can do for yourself.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...