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?
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.
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"
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.
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.
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.
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.
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
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.
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:
Public Class SomeType
Public Sub DoSomething()
Dim someValue As String = My.Settings.SomeSetting
End Sub
End Class
In your application you must be creating an instance of that class and invoking that member, e.g.
vb.net Code:
Dim someObject As New SomeType
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:
Public Class SomeType
Public Sub DoSomething(ByVal someSetting As String)
Dim someValue As String = someSetting
End Sub
End Class
vb.net Code:
Dim someObject As New SomeType
someObject.DoSomething(My.Settings.SomeSetting)
or:
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 Class
vb.net Code:
Dim someObject As New SomeType
someObject.SomeSetting = My.Settings.SomeSetting
someObject.DoSomething()
That is no different to what someone with nearly 4000 posts must have done many, many times before.
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.
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:
Public Class SomeType
Public Sub DoSomething()
Dim someValue As String = My.Settings.SomeSetting
End Sub
End Class
In your application you must be creating an instance of that class and invoking that member, e.g.
vb.net Code:
Dim someObject As New SomeType
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:
Public Class SomeType
Public Sub DoSomething(ByVal someSetting As String)
Dim someValue As String = someSetting
End Sub
End Class
vb.net Code:
Dim someObject As New SomeType
someObject.DoSomething(My.Settings.SomeSetting)
or:
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 Class
vb.net Code:
Dim someObject As New SomeType
someObject.SomeSetting = My.Settings.SomeSetting
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.