By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,693 Members | 1,267 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,693 IT Pros & Developers. It's quick & easy.

Reflection, and an error

P: n/a
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
Feb 7 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Babillon wrote:
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.
Feb 8 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.