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?
Printable View
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?
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...
This is with 2008 not 2005 but I doubt it has changed.Code:Public Class Class1
Public Sub TestSetting()
Debug.Print(My.Settings.TestSetting)
End Sub
End Class
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:
Private Function GetDBConInfo() As String Dim sDBConInfo As String = Nothing Dim sSectionToGet As String = "usersettings/SupportSpaceCallTracker.My.MySettings" Dim sAppName As String = "SupportSpaceCallTracker.exe" Dim sSettingToGet As String = "NewConnectionString" Try Dim config As System.Configuration.Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(Application.ExecutablePath & "\" & sAppName) Dim section As System.Configuration.ClientSettingsSection = CType(config.GetSection(sSectionToGet), Configuration.ClientSettingsSection) sDBConInfo = section.Settings.Get(sSettingToGet).Value.ValueXml.InnerText Catch ex As Configuration.ConfigurationErrorsException MessageBox.Show(ex.Message, _ "Configuration Errors Exception.", _ MessageBoxButtons.OK, _ MessageBoxIcon.Error, _ MessageBoxDefaultButton.Button1, _ MessageBoxOptions.DefaultDesktopOnly, _ False) End Try Return sDBConInfo End Function
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.
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:
Public Interface DBInterface Function GetDatabaseConnectionString(ByVal DBConString As String) As String End Interface
I then went into the class library file and the screenshot shows the rest. I am not understanding this at all.
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.
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:
Public ReadOnly Property GetConnectionString() As String Get Return My.Settings.NewConnectionString End Get End Property
Then I went into the class for my class library and wrote the following:
vb Code:
Public ReadOnly Property SetConnectionString() As String Get Return getconnectionstring End Get End Property
and I am told:
Quote:
Name 'GetConnectionString' is not declared.
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.
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!
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.In your application you must be creating an instance of that class and invoking that member, e.g.vb.net Code:
Public Class SomeType Public Sub DoSomething() Dim someValue As String = My.Settings.SomeSetting End Sub End ClassNow, 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:
Dim someObject As New SomeType someObject.DoSomething()vb.net Code:
Public Class SomeType Public Sub DoSomething(ByVal someSetting As String) Dim someValue As String = someSetting End Sub End Classor:vb.net Code:
Dim someObject As New SomeType someObject.DoSomething(My.Settings.SomeSetting)vb.net Code:
Public Class SomeType Private _someSetting As String Public Property SomeSetting() As String Get Return _someSetting End Get Set(ByVal value As String) _someSetting = value End Set End Property Public Sub DoSomething() Dim someValue As String = SomeSetting End Sub End ClassThat is no different to what someone with nearly 4000 posts must have done many, many times before.vb.net Code:
Dim someObject As New SomeType someObject.SomeSetting = My.Settings.SomeSetting someObject.DoSomething()
Nope, I have only been working with properties for a few days. never understood how they worked.
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:and:Quote:
add one more property or 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.Quote:
set that property or call that method