Custom Controls with object type properties.

G

Guest

Guys,

I've written a custom control (or at least tweaked someone else's!) in C# to
be used on forms (rather than asp.net forms). The control is a date time
picker, but its Value property returns a special date time object we have
written, when a date is selected. The property object's class is called
OptionalDateTime. This is basically to allow blank dates, and is useful when
a date to be entered is not mandatory.

This is the problem: If I draw the control on a form, it works fine, and I
can read the Value property of my control using the Value property. However,
for some reason, it suddenly stops working at random!

I think the problem is to do with code created automatically in the
InitializeComponent() method. When the control is first drawn on the form,
this gets created:

this.lockableOptionalLightDateTimePicker1.Value =
((HCS.DataTypes.Time.OptionalDateTime)(resources.GetObject("lockableOptionalLightDateTimePicker1.Value")));

When it stops working, the code is the same as listed here, but I start
getting an error with this line of code. The problem is hard to recreate
reliably at the moment, but I am working on it. I’m pretty sure the error is
the “Object reference not set to an instance of an object†exception, but I
will confirm this.

Has anyone got any ideas? Is it bad practice to have a Value property that
returns an object type? I wouldn’t have thought so, since other Microsoft
controls do this; for instance, when colors are involved.

Your help in this matter would be greatly appreciated!

Thanks,

Steve.
 
K

Kevin Yu [MSFT]

Hi Steve,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you are receiving a
NullReferenceException when using your own control on a windows form app.
The exception seems to be thrown from InitializeComponent method. If there
is any misunderstanding, please feel free to let me know.

From the exception message, I think there might be some references that
might not point to a valid object in the following statement.

this.lockableOptionalLightDateTimePicker1.Value =
((HCS.DataTypes.Time.OptionalDateTime)(resources.GetObject("lockableOptional
LightDateTimePicker1.Value")));

For troubleshooting, I think you can try to set a breakpoint when the
exception is thrown and add the following watches in the watch window.

this.lockableOptionalLightDateTimePicker1
resources
resources.GetObject("lockableOptionalLightDateTimePicker1.Value")

If one of them is a null reference, please check if you have set it to a
valid object before. HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
G

Guest

Kevin,

Thanks for your prompt reply!

As I say, we haven't been able to find a sure way to get an exact repro yet,
although we are working on it. I just wondered if anyone could tell what was
wrong without an exact repro!

Yes, your summary of the problem is correct; we have written a control. When
the control is placed on a Windows form, the following code (and some other
property setting code) is written to the InitializeComponent method:

this.lockableOptionalLightDateTimePicker1.Value =
((HCS.DataTypes.Time.OptionalDateTime)(resources.GetObject("lockableOptionalLightDateTimePicker1.Value")));

When the form is then run, the form and the control both work correctly.

However, after an in determinant length of time, this exact same line of
code starts to cause problems! When the form’s InitializeComponent fires,
this exact same line of code causes an exception. Now again, because the
problem is hard to repro, I cannot confirm that the exception is a
NullReferenceException, although I’m pretty sure that is what it was. As soon
as I can get a repro, I’ll update this post.

The thing is, this line of code is looking at a resource file to find out
what value to initially give to my date picker control. It is strange how it
can work fine for a while and then stop working; I never directly edit the
forms resource file, and so the entry against the control’s Value property
should not change! Why does it stop working? When I can get a repro, I’ll try
your suggestion.

Have you any other suggestions in the meantime? I wondered if you need to
specify an attribute within the control to show that a property takes a
reference type rather than a value type?

Thanks again for your help, and apologies for being so vague. I’ll pin
things down more when I can get a repro.

Many thanks,

Steve.
 
K

Kevin Yu [MSFT]

Hi Steve,

Thanks for you clarify! Besides the advice from my last post. I think you
can also check the other parts of the code to see if any code has cleared
the resource lockableOptionalLightDateTimePicker1. Also I don't think we
need to put .Value follows the resource name. Is .Value a part of resource
name? If not, please try to remove it.

If that still doesn't help, I would be happy to see a repro. Thanks!

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 

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