Reflection, and an error

B

Babillon

Good day all. I have a bit of a problem in a small project I'm working on.

I've decided to make a function that will recursively loop through all
of the controls on a form and apply a user specified property to them
e.g.; Enabled = False

I found out about Reflection and it seemed the best way to do what I
wanted done. With that I built my function, and it compiled correctly.
However, when I go to actually use the function, I get an error that
states: Cannot widen type to primitive type.

At first I thought it was because I didn't pass the SetProperty function
a null like it asked, then I gave it the null and the error kept coming up.

If anyone would be able to point out my mistake, I'd be very
appreciative. Below is the code in question.


Imports System.Reflection


Module ControlMods
Public Function setParameters(ByRef controls As
Control.ControlCollection, ByVal mode As String, ByVal value As Object)
As Boolean
Dim controlObject As Object
Dim t As Type
Dim list() As PropertyInfo
Dim item As PropertyInfo


For Each controlObject In controls
If CType(controlObject, Control).Controls.Count > 0 Then
setParameters(controlObject, mode, value)
End If
t = controlObject.GetType()
list = t.GetProperties
For Each item In list
If item.Name = mode Then
item.SetValue(controlObject, value, Nothing)
End If
Next
Next
End Function
End Module
 
B

Babillon

Babillon said:
Good day all. I have a bit of a problem in a small project I'm working on.

I've decided to make a function that will recursively loop through all
of the controls on a form and apply a user specified property to them
e.g.; Enabled = False

I found out about Reflection and it seemed the best way to do what I
wanted done. With that I built my function, and it compiled correctly.
However, when I go to actually use the function, I get an error that
states: Cannot widen type to primitive type.

At first I thought it was because I didn't pass the SetProperty function
a null like it asked, then I gave it the null and the error kept coming up.

If anyone would be able to point out my mistake, I'd be very
appreciative. Below is the code in question.


Imports System.Reflection


Module ControlMods
Public Function setParameters(ByRef controls As
Control.ControlCollection, ByVal mode As String, ByVal value As Object)
As Boolean
Dim controlObject As Object
Dim t As Type
Dim list() As PropertyInfo
Dim item As PropertyInfo


For Each controlObject In controls
If CType(controlObject, Control).Controls.Count > 0 Then
setParameters(controlObject, mode, value)
End If
t = controlObject.GetType()
list = t.GetProperties
For Each item In list
If item.Name = mode Then
item.SetValue(controlObject, value, Nothing)
End If
Next
Next
End Function
End Module
Ahhh... Nevermind I fixed it. Though, if anyone knows of a way to
restrict the items the function effects (like say passing the type of
control for it to work on), I would appreciate the point in the right
direction.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top