Results 1 to 16 of 16

Thread: [RESOLVED] Access My.Settings in a Class Library

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Resolved [RESOLVED] Access My.Settings in a Class Library

    Hi all,

    I have created a Class Library project to create a VB.net 2005 DLL file. I am trying to access My.Settings in this class library project and not able to. How would I do this?

  2. #2
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Access My.Settings in a Class Library

    How are you trying to do it?

    I've created an new class library project with a test setting called (spookily) "TestSetting", and then within a class tried to access it and it works fine...

    Code:
    Public Class Class1
    
        Public Sub TestSetting()
            Debug.Print(My.Settings.TestSetting)
        End Sub
    
    End Class
    This is with 2008 not 2005 but I doubt it has changed.

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Access My.Settings in a Class Library

    OK, I am revisiting this issue as I am still not able to get it to work. I am wanting to share the applications app.config with the class library. No point having the same information in multiple places.

    This is what I have so far and I am told the EXE can't be found. Any ideas?
    vb Code:
    1. Private Function GetDBConInfo() As String
    2.         Dim sDBConInfo As String = Nothing
    3.         Dim sSectionToGet As String = "usersettings/SupportSpaceCallTracker.My.MySettings"
    4.         Dim sAppName As String = "SupportSpaceCallTracker.exe"
    5.         Dim sSettingToGet As String = "NewConnectionString"
    6.  
    7.         Try
    8.             Dim config As System.Configuration.Configuration
    9.             config = System.Configuration.ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath & "\" & sAppName)
    10.             Dim section As System.Configuration.ClientSettingsSection = CType(config.GetSection(sSectionToGet), Configuration.ClientSettingsSection)
    11.             sDBConInfo = section.Settings.Get(sSettingToGet).Value.ValueXml.InnerText
    12.         Catch ex As Configuration.ConfigurationErrorsException
    13.             MessageBox.Show(ex.Message, _
    14.                             "Configuration Errors Exception.", _
    15.                             MessageBoxButtons.OK, _
    16.                             MessageBoxIcon.Error, _
    17.                             MessageBoxDefaultButton.Button1, _
    18.                             MessageBoxOptions.DefaultDesktopOnly, _
    19.                             False)
    20.         End Try
    21.  
    22.         Return sDBConInfo
    23.     End Function

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

    Re: Access My.Settings in a Class Library

    What you're trying to do is wrong. The library should absolutely not be trying to access My.Settings. A library should be a completely self-contained chunk of executable code. You should be able to use that same class library in any application or library you want. There should not be any dependency in the library on any other assembly that may be using it. Dependencies should go one way only. You should never have two assemblies that each depend on the other.

    If your library needs data then you should provide an interface, e.g. method or property, where that data can be passed in. Your application can then get the data from its own My.Settings and pass the data into the library. The library should care where the data comes from. You should be able to reference that same library in another application and pass the data in from another source and the library should work as expected and be none the wiser.
    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
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Access My.Settings in a Class Library

    Quote Originally Posted by jmcilhinney View Post
    What you're trying to do is wrong. The library should absolutely not be trying to access My.Settings. A library should be a completely self-contained chunk of executable code. You should be able to use that same class library in any application or library you want. There should not be any dependency in the library on any other assembly that may be using it. Dependencies should go one way only. You should never have two assemblies that each depend on the other.

    If your library needs data then you should provide an interface, e.g. method or property, where that data can be passed in. Your application can then get the data from its own My.Settings and pass the data into the library. The library should care where the data comes from. You should be able to reference that same library in another application and pass the data in from another source and the library should work as expected and be none the wiser.
    I am reading up about interfaces and it doesn't make any sense to me. Might as well me written in Chinese.

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Access My.Settings in a Class Library

    OK,

    In my main project, I added a new interface file. I called it DBInterface.vb. I then went into the file and did the following:
    vb Code:
    1. Public Interface DBInterface
    2.     Function GetDatabaseConnectionString(ByVal DBConString As String) As String
    3. End Interface

    I then went into the class library file and the screenshot shows the rest. I am not understanding this at all.
    Attached Images Attached Images  

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

    Re: Access My.Settings in a Class Library

    I didn't mean that type of interface. I'm talking about the API of the library, i.e. the public types it exposes and their public members. For instance, instead of your library reading directly from My.Settings, it exposes a public property, the application reads the value from My.Settings and assigns the value to that property.
    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

  8. #8

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Access My.Settings in a Class Library

    Quote Originally Posted by jmcilhinney View Post
    I didn't mean that type of interface. I'm talking about the API of the library, i.e. the public types it exposes and their public members. For instance, instead of your library reading directly from My.Settings, it exposes a public property, the application reads the value from My.Settings and assigns the value to that property.

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

    Re: Access My.Settings in a Class Library

    This is elementary stuff. You already have an application project, right? You already have a library project, right? The application is referencing the library, right? In the application, are you creating instances of types declared in the library? Are you setting properties and calling methods of those objects? You must be, so you're already doing what I'm suggesting. You simply need to add one more property or method to an existing type or add a new type. In your application, you create an instance of that type, exactly as you're already doing elsewhere, and set that property or call that method, just as you're already doing elsewhere. Your library now contains the data it needs, without having to get it from My.Settings.
    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

  10. #10

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Access My.Settings in a Class Library

    Quote Originally Posted by jmcilhinney View Post
    This is elementary stuff. You already have an application project, right? You already have a library project, right? The application is referencing the library, right? In the application, are you creating instances of types declared in the library? Are you setting properties and calling methods of those objects? You must be, so you're already doing what I'm suggesting. You simply need to add one more property or method to an existing type or add a new type. In your application, you create an instance of that type, exactly as you're already doing elsewhere, and set that property or call that method, just as you're already doing elsewhere. Your library now contains the data it needs, without having to get it from My.Settings.
    OK, I went into the main project, double-clicked on 'My Project' and then Settings tab and then clicked on the 'View Code' button. I then wrote the following:
    vb Code:
    1. Public ReadOnly Property GetConnectionString() As String
    2.             Get
    3.                 Return My.Settings.NewConnectionString
    4.             End Get
    5.         End Property

    Then I went into the class for my class library and wrote the following:
    vb Code:
    1. Public ReadOnly Property SetConnectionString() As String
    2.         Get
    3.             Return getconnectionstring
    4.         End Get
    5.     End Property

    and I am told:
    Name 'GetConnectionString' is not declared.

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

    Re: Access My.Settings in a Class Library

    You're really trying to make this difficult when it's not. I said that the library is supposed to be unaware of the application, yet you're still trying to access a property declared in the application from the library. I specifically said, more than once, that declare a property in the library and then you write code in the application to set that property. The application knows about the library but not the other way around, so the application MUST be the one to instigate the operation.

    First of all, you wouldn't have properties named GetConnectionString and SetConnectionString. Those are method names. A property would just be named ConnectionString. Secondly, your SetConnectionString property has a getter and no setter, so the name is doubly wrong. You need to declare a ConnectionString property in your library and then your application needs to set that property, getting the value from My.Settings first. It's that simple.
    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

  12. #12

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Access My.Settings in a Class Library

    I don't understand any of this. Maybe I am dumb. I am not able to figure out how to pass the information to the class library!

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

    Re: Access My.Settings in a Class Library

    This doesn't make sense to me. What use could this class library be if you can't pass data to it? You must already be passing data to it or it couldn't be doing anything useful. Why is it such a challenge to declare a property and set it's value?

    I'll break it down to absolute basics. Your library must contain at least one type with at least one member. In that one member you are trying to access My.Settings, e.g.
    vb.net Code:
    1. Public Class SomeType
    2.  
    3.     Public Sub DoSomething()
    4.         Dim someValue As String = My.Settings.SomeSetting
    5.     End Sub
    6.  
    7. End Class
    In your application you must be creating an instance of that class and invoking that member, e.g.
    vb.net Code:
    1. Dim someObject As New SomeType
    2.  
    3. someObject.DoSomething()
    Now, you cannot access My.Settings in the library. The application must pass the data into the object whose type is declared in the library, e.g.
    vb.net Code:
    1. Public Class SomeType
    2.  
    3.     Public Sub DoSomething(ByVal someSetting As String)
    4.         Dim someValue As String = someSetting
    5.     End Sub
    6.  
    7. End Class
    vb.net Code:
    1. Dim someObject As New SomeType
    2.  
    3. someObject.DoSomething(My.Settings.SomeSetting)
    or:
    vb.net Code:
    1. Public Class SomeType
    2.  
    3.     Private _someSetting As String
    4.  
    5.     Public Property SomeSetting() As String
    6.         Get
    7.             Return _someSetting
    8.         End Get
    9.         Set(ByVal value As String)
    10.             _someSetting = value
    11.         End Set
    12.     End Property
    13.  
    14.     Public Sub DoSomething()
    15.         Dim someValue As String = SomeSetting
    16.     End Sub
    17.  
    18. End Class
    vb.net Code:
    1. Dim someObject As New SomeType
    2.  
    3. someObject.SomeSetting = My.Settings.SomeSetting
    4. someObject.DoSomething()
    That is no different to what someone with nearly 4000 posts must have done many, many times before.
    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

  14. #14

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Access My.Settings in a Class Library

    Nope, I have only been working with properties for a few days. never understood how they worked.

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

    Re: Access My.Settings in a Class Library

    You have posts in the VB.NET forum going back to the beginning of 2007 so you've definitely been setting properties for a long time, even if not declaring them. Regardless, I did say:
    add one more property or method
    and:
    set that property or call that method
    I know that you've been declaring methods for a lot longer than a few days. Anyway, I think that you were overthinking it, trying to turn it into something that was more complex than it was. Declare a member and invoke it, same as you've done a thousand times, in VB6 as well as VB.NET.
    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

  16. #16

    Thread Starter
    PowerPoster
    Join Date
    Apr 2005
    Location
    Debug.Print
    Posts
    3,885

    Re: Access My.Settings in a Class Library

    Quote Originally Posted by jmcilhinney View Post
    This doesn't make sense to me. What use could this class library be if you can't pass data to it? You must already be passing data to it or it couldn't be doing anything useful. Why is it such a challenge to declare a property and set it's value?

    I'll break it down to absolute basics. Your library must contain at least one type with at least one member. In that one member you are trying to access My.Settings, e.g.
    vb.net Code:
    1. Public Class SomeType
    2.  
    3.     Public Sub DoSomething()
    4.         Dim someValue As String = My.Settings.SomeSetting
    5.     End Sub
    6.  
    7. End Class
    In your application you must be creating an instance of that class and invoking that member, e.g.
    vb.net Code:
    1. Dim someObject As New SomeType
    2.  
    3. someObject.DoSomething()
    Now, you cannot access My.Settings in the library. The application must pass the data into the object whose type is declared in the library, e.g.
    vb.net Code:
    1. Public Class SomeType
    2.  
    3.     Public Sub DoSomething(ByVal someSetting As String)
    4.         Dim someValue As String = someSetting
    5.     End Sub
    6.  
    7. End Class
    vb.net Code:
    1. Dim someObject As New SomeType
    2.  
    3. someObject.DoSomething(My.Settings.SomeSetting)
    or:
    vb.net Code:
    1. Public Class SomeType
    2.  
    3.     Private _someSetting As String
    4.  
    5.     Public Property SomeSetting() As String
    6.         Get
    7.             Return _someSetting
    8.         End Get
    9.         Set(ByVal value As String)
    10.             _someSetting = value
    11.         End Set
    12.     End Property
    13.  
    14.     Public Sub DoSomething()
    15.         Dim someValue As String = SomeSetting
    16.     End Sub
    17.  
    18. End Class
    vb.net Code:
    1. Dim someObject As New SomeType
    2.  
    3. someObject.SomeSetting = My.Settings.SomeSetting
    4. someObject.DoSomething()
    That is no different to what someone with nearly 4000 posts must have done many, many times before.
    I had to read your post a few times and then thought that it made sense finally. Tried it and it didn't give me any errors, loaded the app (F5) and everything worked the way as intended. Thanks for your guidance jmc. I appreciate your expertise.

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