Results 1 to 7 of 7

Thread: [RESOLVED] HELP/UNDERSTANDING: Custom application settings class interacting with my form

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2005
    Posts
    139

    Resolved [RESOLVED] HELP/UNDERSTANDING: Custom application settings class interacting with my form

    Hi,

    I wasn't sure the best place to post this, so feel free to move it to the appropriate place.

    Anyway, I'm new to C# an OOP and I was having a problem with an application I am writing that runs in the task tray using the Notify Icon component.

    I created a class that I am using to handle application configuration data. This class is small and has the following properties and methods. I also want to make this class serializable, except the IsDirty flag. I realize I may need to write custom code to serialize eveything but this property.

    Properties:
    int RefreshDuration
    string URL
    string ExcludeList

    Methods:
    void LoadSettings
    void SaveSettings
    bool IsDirty

    In my main form I created an instance of the Settings class and I can set the various properties and call the various methods without any problems. I am have problems when I want to access components on my main form from this class and I am not sure if this is the best way to do it since the Settings class will need to be changed if I ever want to use it in another project.

    Here's what I am wanting to happen:

    1. LoadSettings is called on application startup.
    2. The user changes any setting and I want to set the IsDirty property to true, which in turn enables the Ok button on the settings form.
    2.1 If the user clicks Ok, then I want to call SaveSettings, set IsDirty to false, which disables the Ok button.
    2.2 The user closes the form and get prompted to Save changes, with Yes/No/Cancel message box.
    2.2.1 If the user clicks Yes, then I want to do 2.1
    2.2.2 If the user clicks No, then want to set IsDirty to false, which disables the Ok button.
    2.2.3 The message box closes and the settings form remains and IsDirty still = false.

    However, when I try to code step 2, then I can't seem to access the main form controls, such as the Ok button, to enable/disable it. Somehow the IsDirty property doesn't seem like the best place to enable/disable the button anyway.

    I have attached a small sample project and if anyone could help explain the best way to create a reusable settings class I would appreciate it.

    Also, I don't mind using a true application config file for my settings, but just to understand how I would accomplish the above would be great.

    Thanks,
    CT
    Attached Files Attached Files

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: HELP/UNDERSTANDING: Custom application settings class interacting with my form

    Firstly, you've got IsDirty listed as a method there, yet you keep referring to it as a property. Note that variables are NOT properties. A variable is either a variable or a field. Properties are something else in .NET. They are a hybrid between variables and methods. They appear from the outside to behave like fields but on the inside they behave like methods. Methods are just methods.

    Finally, your settings class should ABSOLUTELY NOT be accessing any controls on any form. All that class should know about is loading, changing and saving settings. It should be up to the form to enable or disable its own buttons. Having said that, you say you want to prompt the user to save the settings when they close the form and disable the OK button if they say No. The form is closed so what point disabling any buttons on it?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Dec 2005
    Posts
    139

    Re: HELP/UNDERSTANDING: Custom application settings class interacting with my form

    Quote Originally Posted by jmcilhinney
    Firstly, you've got IsDirty listed as a method there, yet you keep referring to it as a property. Note that variables are NOT properties. A variable is either a variable or a field. Properties are something else in .NET. They are a hybrid between variables and methods. They appear from the outside to behave like fields but on the inside they behave like methods. Methods are just methods.?
    You are correct, I keep referring to it as a property, but in the sample project it is defined as a property yet have it listed above as a method. I understand the differences between a property and a method, I just mistyped it, sorry about that.

    Quote Originally Posted by jmcilhinney
    Finally, your settings class should ABSOLUTELY NOT be accessing any controls on any form. All that class should know about is loading, changing and saving settings. It should be up to the form to enable or disable its own buttons.?
    Thanks, that's what I was mainly wanting to know. I guess I was just over complicating the process.

    Quote Originally Posted by jmcilhinney
    Having said that, you say you want to prompt the user to save the settings when they close the form and disable the OK button if they say No. The form is closed so what point disabling any buttons on it?
    Although it's not in the sample project I was referring to the FormClosing event. If any changes occurred to the settings screen I wanted to give the user the chance to save the changes before discarding them. I hope that I explained this better. Either way, since I won't be accessing the form controls from the Settings class I should be fine with the code that I have for the FormClosing event.

    On a side note, what would be the best method to handle the settings now? Should I just use the application config approach, use my class and serialize it to disk or something else?

    Oh, btw jmcilhinney, I posted this on vbdotnetforums.com and I know you frequent that site as well. Just disregard my post there.

    Thanks,
    CT

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: HELP/UNDERSTANDING: Custom application settings class interacting with my form

    I would suggest that you use the application settings functionality built into .NET 2.0 unless you have a specific reason not to. Application settings can't do everything so there will be times that you need to store data somewhere else. You should generally store your own binary or XML serialised objects in the application data folder, either for all user's or the current user. Given that each application is likely to have different needs for settings you might find that a relatively bare base class is all you can create without knowing the application it will be used in.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Dec 2005
    Posts
    139

    Re: HELP/UNDERSTANDING: Custom application settings class interacting with my form

    Quote Originally Posted by jmcilhinney
    I would suggest that you use the application settings functionality built into .NET 2.0 unless you have a specific reason not to..
    That sounds like a good starting place.

    Quote Originally Posted by jmcilhinney
    Given that each application is likely to have different needs for settings you might find that a relatively bare base class is all you can create without knowing the application it will be used in.
    Would creating a generic base class for settings that I can inherit and build upon in other projects be the way to go?

    Thanks,
    CT

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: HELP/UNDERSTANDING: Custom application settings class interacting with my form

    Quote Originally Posted by Cavar
    Would creating a generic base class for settings that I can inherit and build upon in other projects be the way to go?

    Thanks,
    CT
    You'd have to consider what common functionality they would have that you could implement in a base class. Probably not much I'd guess. There's not really any call for polymorphism either so I can't see that you'd get much benefit.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Dec 2005
    Posts
    139

    Re: HELP/UNDERSTANDING: Custom application settings class interacting with my form

    Ok, thanks jmcilhinney. I'll start cracking some books and web tutorials and do some learnin'.

    CT

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width