Application Settings - Null value

D

David Veeneman

I am using application settings in C# 2.0 to save a form's size. I used this
code to retrieve the setting in the FormLoad event:

// Set window size
if (Properties.Settings.Default.FormSize != null)
{
this.Size = Properties.Settings.Default.FormSize;
}

The code generated a runtime error, saying that the property was null, and
that I should check to see if the property is null before calling it. Which,
of course, is exactly what I'm doing!

How do I test the setting to see if it has a value? Thanks in advance.
 
D

David Veeneman

I found my answer. When I created the settings in the Designer (the
project's Properties pages), I hadn't entered a default value for the
FormSize setting I created--I had left the Value column blank. As a result,
the designer code was declaring the setting but not instantiating it.

The moral of the story is: Always declare a default value for any property
created in the designer.
 

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