How to get app.config info from code in a referenced class

R

Robert Dufour

I have a winforms project Vs2005 with one project - Project1 which is the
startup project and clsProject2 which is a class that is being used by
Project1 (an exception handler). In clsProject2 I have a procedure that must
take information from the app.config file of Project1. In the clsproject2 I
need to write code that gets the value of a setting in the app.config file
of Project1. I find that the code I had that seemed to work ijn Vs2003 now
no longer picks up the data in the Project1 app.config. Exactly how would I
be able to use code in clsProject2 to get configuration info from Project1
app.config.
I want to try to get the info via clsProject2 so that I can reuse the same
class without writing too much code in the calling projects to come later.

Any help would be really appreciated.

Thanks for any help.
Bob
 
G

Guest

Exactly how would I
be able to use code in clsProject2 to get configuration info from
Project1 app.config.
I want to try to get the info via clsProject2 so that I can reuse the
same class without writing too much code in the calling projects to
come later.

clsProject2 is a library right?

In that case it'll run in the appDomain of it's parent process - thus it'll
use the parent's app.config.

So just go AppSetting("MyKey") - it'll pick it up from Project1's
app.config.
 
R

Robert Dufour

Thanks
Bob

Spam Catcher said:
clsProject2 is a library right?

In that case it'll run in the appDomain of it's parent process - thus
it'll
use the parent's app.config.

So just go AppSetting("MyKey") - it'll pick it up from Project1's
app.config.
 
R

Robert Dufour

Hi, when I try that
Dim strTemp As String = AppSettings("mysetting")

I get the wavy blue underlines and "appsettings is a type and cannot be
used as an expression".

Bob
 
G

Guest

Hi, when I try that
Dim strTemp As String = AppSettings("mysetting")

I get the wavy blue underlines and "appsettings is a type and cannot be
used as an expression".

You need to import the ConfigurationSettings class (System.Configuration)
or the new Configuration Manager class (you need to reference the
System.Configuration DLL).
 
R

Robert Dufour

Thanks for your help. Still having a problem though. I referenced the new
configuration manager class. The wavy warning is gone and my code executes
without any exceptions, BUT it does not pick up any of the values in the
config file.
When I single step over the following line in the debugger strTemp remains
empty but there is a value for it in the config file.
Dim strTemp As String = AppSettings.GetString(strKey) - strkey is a
variable passed to this procedure, the spelling of the variable value is
correct.

Isn't high tech fun?

Thanks for your help

Bob
 
R

Robert Dufour

Simple sample demonstrating the problem consists of a solution containing a
class project and a windows form project. I put the same in a new thread
also, hope thats OK.

Here's the class code snippet
Imports System.Configuration.ConfigurationManager

Public Class Class1

Public _strTestSetting As String



Public Sub SetTestsetting()

_strTestSetting = AppSettings.Get("TestSetting") -should get the value of
TestSetting in app config file

End Sub


End Class

This is the snippet in the project calling the class -Project TestIt - To
test you should have a setting named TestSetting application scope, value
Testvalue

Imports System.Configuration

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Dim mycls As New clsTestconfig.Class1

mycls.SetTestsetting()

Label1.Text = mycls._strTestSetting

End Sub

End Class

There's a setting TestSetting in the settings file of the project Testit

Put a breakpoint on line

_strTestSetting = AppSettings.Get("TestSetting")

Click the button, you will see as you step over the breakpoint that the
value of _strTestSetting stays at nothing. ie, its not picking up the value
of TestSetting in app.config.

How can I get this pickup of the value to work? By the way I tested in the
IDE and the exe itself. Both failed same way.

Thanks for any help

Bob
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top