Question PropertyGrid PropertyValueChanged SetValue not working in .NET 4.0

tim8w

Active member
Joined
Oct 26, 2010
Messages
38
Programming Experience
5-10
I have seen great examples here and had great hopes that it would work for our application. I need to catch a change in the GridItem and revert it under certain conditions. We are using VB.Net 2012 with .NET Framework 4.0. All the examples I have been able to find use the following code:

VB.NET:
Dim iChangedItem = e.ChangedItem
Dim propertyName = iChangedItem.PropertyDescriptor.Name
Dim pInfo As _PropertyInfo = 
pgSetup.SelectedObject.GetType().GetProperty(propertyName)
pInfo.SetValue(pgSetup.SelectedObject, e.OldValue, null)


If I do this I get the following message from VisualStudio:

'null' is not declared. 'Null' constant is no longer supported. Use 'System.DBNull' instead.

If I use 'System.DBNull.Value' I get a different error:

Value of type 'System.DBNull' cannot be converted to '1-dimensional array of Object'

I have also tried 'Nothing", which VisualStudio is OK with but I get a runtime error of:

NullReferenceException was unhandled

Any ideas how I can get this to work? I know that there is a version with only 2 parameters in later versions of the .NET Framework, but as of today anyway, I cannot change the .NET platform for this program.
 
pInfo declared _PropertyInfo is not correct, it should be PropertyInfo. The first is in Runtime.InteropServices namespace and the latter is in Reflection namespace.

When docs say "This value should be null for non-indexed properties." for the third parameter of SetValue that means Nothing in VB.

I have also tried 'Nothing", which VisualStudio is OK with but I get a runtime error of:

NullReferenceException was unhandled
NullReferenceException is not due to the parameter, it is one of the qualifying expressions that is a null reference as underlined here:
pInfo.SetValue(pgSetup.SelectedObject, e.OldValue, null)
You should be able to see which by using the VS debugger (mouse over each when the stop occurs), but it should resolve anyway when you get pInfo right.
 
NullReferenceException is not due to the parameter, it is one of the qualifying expressions that is a null reference as underlined here:

pInfo.SetValue(pgSetup.SelectedObject, e.OldValue, null)

You should be able to see which by using the VS debugger (mouse over each when the stop occurs), but it should resolve anyway when you get pInfo right.

Thanks, I fixed the namespace problem as you suggested. It still has the other problem above. VS will not take 'null' and if I leave 'Nothing' there I still get a runtime error on the SetValue line.

Details say: "Object reference not set to an instance of an object." for the 'Nothing' parameter.

Thanks,
Tim
 
A null for that parameter doesn't throw NullReferenceException says documentation (PropertyInfo.SetValue Method (System.Reflection) | Microsoft Docs), something else is a null reference.
NullReferenceException occurs when you try to use a method or property of a reference type whose value is null.
Let's say you have a variable A and want to convert it to string, you code A.ToString, if A at the time of the call is null that means the attempt at doing 'null'.ToString result in NullReferenceException.
Troubleshooting Exceptions: System.NullReferenceException
 
I understand that it's not looking for 'null' or 'Null' or 'Nothing' to be passed for the third variable. Your link shows that it is looking for 'object[] index'. What do I put for the third parameter?

Thanks,
Tim Alvord
 
OK, I must be an Idiot. Nothing I try works here. Can you explain exactly what I need to pass for the third parameter?

VB.NET:
Dim indexValue As Object = New Object
pInfo.SetValue(pgSetup.SelectedObject, e.OldValue, indexValue)

I've tried everything I could think of. I tried:

Dim indexValue As Object = Nothing

Dim indexValue[] As Object = Nothing

Nothing works. This should be straight forward and easy, obvious, but I'm just not getting it...


Tim Alvord
 
Here you can see two typical null reference exception and debugging them, the first I intentionally set pInfo to Nothing, mouse over pInfo during the stop shows it is Nothing.
nullref1.png

Here I used an inline object with a property, but managed to set the variable to null before it was used, again using the debugger I can see that 'anon' is Nothing so that obviously must throw NullReferenceException.
nullref2.png

The exception window also says which reference is the problem in both these cases, but I'm sure previous VS versions were not so detailed.
 
Ok. I see where the problem is now. pInfo was Nothing for some reason. I got the info from the input parameter sender, but for some reason when it tries to set pInfo, the GetProperty returns Nothing.

At least I have somewhere else to look now...

Thanks,
Tim Alvord
 
With the help from a user in another Forum, this is the solution that ended up working:

VB.NET:
Dim propertyName = e.ChangedItem.PropertyDescriptor.Name
Dim pInfo As PropertyInfo = pgSetup.SelectedGridItem.Parent.Value.GetType().GetProperty(propertyName)

pInfo.SetValue(pgSetup.SelectedGridItem.Parent.Value, e.OldValue, Nothing)
 
Back
Top