My.Settings Values Read Only

Hello All,

I have a bit of a problem here and I can't seem to figure it out. My project is a Class Library. This Class Library is one project which is part of a larger solution. I have several application settings which are set up as part of the project. Within the Class Library's root class, Application, there are two methods. GetSettings and SaveSettings. GetSettings retreives the My.Settings values and sets them as various property values within the Application class. This part works fine. The SaveSettings method retreives the property values from the class and attempts to place them back into My.Settings and then execute My.Settings.Save. When I place the approriate code in the method to update the My.Settings values, I receive an error saying that the My.Settings values are read only. I do not understand what is happening here. It makes no sense for these values to be read only. I would be grateful if someome would be kind enough to shed some light on what is happening here. I am about ready to pull my hair out over this. I have included the GetSettings and SaveSettings routines so that you can see more clearly what I am trying to do.

Public Sub GetSettings()

DBName = My.Settings.DatabasePath

MDWName = My.Settings.SecurityPath

ShowToolStrip = My.Settings.ShowToolStrip

ShowStatusStrip = My.Settings.ShowStatusStrip

ShowTips = My.Settings.ShowTips

ShowSplash = My.Settings.ShowSplash

End Sub

Public Sub SaveSettings()

My.Settings.DatabasePath = DBName

My.Settings.SecurityPath = MDWName

My.Settings.ShowToolStrip = ShowToolStrip

My.Settings.ShowStatusStrip = ShowStatusStrip

My.Settings.ShowTips = ShowTips

My.Settings.ShowSplash = ShowSplash

My.Settings.Save()

End Sub

Thanks in advance for your assistance.

V. Shane Curtis




Answer this question

My.Settings Values Read Only

  • iron94

    Hi,

    This article describes the My.Settings object http://msdn2.microsoft.com/en-us/library/saa62613(VS.80).aspx. Notice this part:

    Scope indicates if the property is read-only. If the value is Application, the property is read-only; if the value is User, the property is read-write.

    You can only save User settings. Maybe you can do this:
    - Define application settings that will be used by all users of the application (read-only).
    - Define user settings that users can overwrite.
    - When loading the settings, if a user setting does not exist, use the appropriate application setting and set the user setting to this value as well; otherwise use the application settings.
    - When saving the settings, save the user settings.

    Best regards,



  • Kbathgate

    Thank you for your help in addressing my problem. It is actually the other way around though, I need to expose the Win Form's Project My.Settings values to the Class Library so that The GetSettings and SaveSettings Method can retreive and update those values and make them available to the Win Forms Project through use of the interfaces provided by the Class Library. I assume then that it will be the Win Form's settings procedure that would be modified as you suggest

    Thanks Again,

    V. Shane Curtis



  • Pete-O

    Hi,

    I'll try to simplify your problem:
    - You have WFProject - a Windows Forms project and CLProject - a class library project.
    - WFProject references CLProject.
    - You want to expose WFProject's Settings object to CLProject.

    To do this, CLProject has to have a reference to WFProject, which cannot be done in this case since WFProject already references CLProject and will create circular dependency.

    Sorry for did not notice this before but I can only suggest you review the design and loosen the dependency between the two projects. Maybe expose CLProject's Settings object to WFProject instead.



  • Suite

    Hi,

    Yes, you can do the same steps on the Windows Form project.



  • Alibong

    Hi,

    You don't need to expose the class library's settings if you're not going to use them.



  • OziSnowman

    Hi,

    You can check to see if there is a class definition of MySettings in D:\Projects\ApplicationFramework\ApplicationFramework\Settings.vb and make it public. Also search for "Class MySettings" in your project files (include hidden files) and make sure they're declared public.

    Note that if you make changes to the settings via the designer, you will have to apply the change to Public again.



  • Shaun Erikson

    Hi,

    In VB 2005 My.Settings has a Friend / Internal scope in its assembly. To expose the settings from your class library to your windows form application, you can do one of the following:

    • Expose public methods / properties from your class library that will inturn modify / save its settings. This will be more work but I would recommend it since you'll have more controls on how you want to expose the settings from your class library.
    • Make some 'hacking' change to your class library's settings:
      • In your Solution Explorer, click Show All Files button.
      • Open My Project | Settings.settings | Settings.Designer.vb
      • Change Partial Friend NotInheritable Class MySettings to Partial Public NotInheritable Class MySettings
      • You now can expose your class library's settings through a shared property.

    Public Shared ReadOnly Property AppSettings() As My.MySettings
    Get
    Return My.Settings
    End Get
    End Property

      • Note that if you edit the settings by the designer the code file will be re-generated and you will need to change it again.

    Best regards,



  • bmc410

    Thank you for your response to my post. Your idea may in fact work. I may give it a shot. For the time being I have gone back to using GetSettings and SaveSettings as those functions do not appear to have been affected by the .Net environment. But back to the original problem. This problem is actually a two-fold problem. My application solution is a multi-project solution. Project one is a WinForms EXE and project two is a Class Library. Project one is a collection of forms and code that form the basis of an Application Framework component. Project two is a series of class modules organized to form an object model. This object model is the workhorse of the application. It manages all data access and manipulation. Project one accesses Project two by declaring a single object variable of type Application. From there other areas of the object model are accessed using standard dot notation, for example, clsApp.Database.InitDB(DBName). The settings should be stored with project one but it is project two which actually manipulates these settings. These projects reside in seperate namespaces. Project One has a reference to project two. The problem is that project two needs to be able to access project one's My.Settings. I have tried several different approaches to solving this problem and cannot find the correct method. How can my overall problem be solved of placing the My.Settings values into project one but allowing project two to handle the saving and retreiving of those settings

    Thanks for your assistance

    V. Shane Curtis



  • Steven Beasley

    Thank you for you continued assistance with my problem. It is very much appreciated. After making the code changes you have recomended, my Class Library project still cannot see the My.Settings values for the Win Forms project. The follow error is generated: Error 3 'DatabasePath' is not a member of 'ApplicationFrameworkCore.My.MySettings'. D:\Projects\ApplicationFrameworkCore\ApplicationFrameworkCore\Application.vb 162 16 ApplicationFrameworkCore
    A similar error is generated for all My.Settings values. I have inclued copies of all code prodedures which have been modified to assist you in determining where the problem may be.

    Namespace My

    'This class allows you to handle specific events on the settings class:

    ' The SettingChanging event is raised before a setting's value is changed.

    ' The PropertyChanged event is raised after a setting's value is changed.

    ' The SettingsLoaded event is raised after the setting values are loaded.

    ' The SettingsSaving event is raised before the setting values are saved.

    Partial Public NotInheritable Class MySettings

    Public Shared ReadOnly Property AppSettings() As My.MySettings

    Get

    Return My.Settings

    End Get

    End Property

    End Class

    End Namespace

    Public Sub GetSettings()

    Dim DefaultDatabasePath As String

    Dim DefaultSecurityPath As String

    Dim DefaultShowToolStrip As Boolean

    Dim DefaultShowStatusStrip As Boolean

    Dim DefaultShowTips As Boolean

    Dim DefaultShowSplash As Boolean

    Dim DefaultLastUser As String

    DefaultDatabasePath = NormalizePath(My.Application.Info.DirectoryPath)

    DefaultSecurityPath = NormalizePath(My.Application.Info.DirectoryPath & "\SYSTEM.MDW")

    DefaultShowToolStrip = True

    DefaultShowStatusStrip = True

    DefaultShowTips = True

    DefaultShowSplash = True

    DefaultLastUser = vbNullString

    DBName = My.Settings.DatabasePath

    If DBName Is vbNullString Then

    My.Settings.DatabasePath()

    DBName = DefaultDatabasePath

    End If

    SecurityPath = My.Settings.SecurityPath

    If SecurityPath Is vbNullString Then

    My.Settings.SecurityPath = SecurityPath

    SecurityPath = DefaultSecurityPath

    End If

    ShowToolStrip = My.Settings.ShowToolStrip

    If ShowToolStrip = CBool(vbNullString) Then

    My.Settings.ShowToolStrip = ShowToolStrip

    ShowToolStrip = DefaultShowToolStrip

    End If

    ShowStatusStrip = My.Settings.ShowStatusStrip

    If ShowStatusStrip = CBool(vbNullString) Then

    My.Settings.ShowStatusStrip = ShowStatusStrip

    ShowStatusStrip = DefaultShowStatusStrip

    End If

    ShowTips = My.Settings.ShowTips

    If ShowTips = CBool(vbNullString) Then

    My.Settings.ShowTips = ShowTips

    ShowTips = DefaultShowTips

    End If

    ShowSplash = My.Settings.ShowSplash

    If ShowSplash = CBool(vbNullString) Then

    My.Settings.ShowSplash = ShowSplash

    ShowSplash = DefaultShowSplash

    End If

    LastUser = My.Settings.LastUser

    If LastUser Is vbNullString Then

    My.Settings.LastUser = LastUser

    LastUser = DefaultLastUser

    End If

    My.Settings.Save()

    End Sub

    Public Sub SaveSettings()

    If DBName <> vbNullString Then

    My.Settings.DatabasePath()

    End If

    If SecurityPath <> vbNullString Then

    My.Settings.SecurityPath = SecurityPath

    End If

    If ShowToolStrip = CBool(vbNullString) Then

    My.Settings.ShowToolStrip = ShowToolStrip

    End If

    If ShowStatusStrip = CBool(vbNullString) Then

    My.Settings.ShowStatusStrip = ShowStatusStrip

    End If

    If ShowTips = CBool(vbNullString) Then

    My.Settings.ShowTips = ShowTips

    End If

    If ShowSplash = CBool(vbNullString) Then

    My.Settings.ShowSplash = ShowSplash

    End If

    If LastUser Is vbNullString Then

    My.Settings.LastUser = LastUser

    End If

    My.Settings.Save()

    End Sub

    Please be advised that this code is a work in progress and I realize that some of it will need to be changed because it is not necessary, but the basic concept is valid.

    Thank you again for you continued assistance.

    Best Regards,

    V. Shane Curtis



  • Scotty Davis

    Hello Again and Thank You for your assistance. I have attempted to use the code you provided. It produces the following error: Error 3 Specified access 'Public' for 'MySettings' does not match the access 'Friend' specified on one of its other partial types. D:\Projects\ApplicationFramework\ApplicationFramework\Settings.vb 9 40 ApplicationFramework

    I think I understand what the error is saying, but I have no idea how to fix it. Would you please be so kind as to enlighten me as to what I need to do to fix the problem

    Best Regards,

    V. Shane Curtis



  • IgorKov

    Is my assumption correct that the class library's settings code would not need to be modified since the settings are in fact going to be located in the Win Forms project or do I need to expose both of these settings

    Thanks,

    V. Shane Curtis



  • My.Settings Values Read Only