tags:

views:

194

answers:

3

I'm using .NET 3.5 and I have a class, A, marked as internal sealed partial and it derives from System.Configuration.ApplicationSettingsBase. I then use an instance of this class in the following manner:

A A_Instance = new A();
A_Instance.Default.Save();

Why would the Visual C# compiler be complaining:

error CS0117: 'A' does not contain a definition for 'Default'

?

A: 

ApplicationSettingsBase inherits from SettingsBase, neither of which expose a Default property. Judging by the compiler error your class doesn't add one.

AFAIK C# does not support any special syntax around the word 'Default' for accessing a property that is marked as the default.

What behaviour are you expecting?

Rob Walker
I really wasn't sure because this code is not my own. It's legacy code.
Mike Caron
+3  A: 

You are probably looking for this:

private static ServerSettings defaultInstance = ((ServerSettings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new ServerSettings())));

public static ServerSettings Default 
 {
    get { return defaultInstance; }
}

This is the code that gets generated by visual studio

Greg Dean
the Synchronized bit didn't work, and ServerSettings should have been 'A' to bit better with the question.
Mike Caron
+1  A: 

.NET doesn't provide a 'Default' member in ApplicationSettingsBase from which to access any methods. What I failed to notice was that 'Default' was being used as a singleton of A. Thus, the sample code provided by gdean2323 gave me some guidance to the fix. The code to fix the problem required me adding the following code to class A (without necessary synchronization for simplicity):

private static A defaultInstance = new A();
public static A Default 
{
    get
    {
        return defaultInstance;
    }
}
Mike Caron