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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Robert Dufour

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

    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


  • Spam Catcher

    #2
    Re: How to get app.config info from code in a referenced class

    "Robert Dufour" <bdufour@sgiims .comwrote in
    news:e9cEfIPCHH A.5012@TK2MSFTN GP06.phx.gbl:
    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("MyK ey") - it'll pick it up from Project1's
    app.config.

    Comment

    • Robert Dufour

      #3
      Re: How to get app.config info from code in a referenced class

      Thanks
      Bob

      "Spam Catcher" <spamhoneypot@r ogers.comwrote in message
      news:Xns987C9B4 6147C3usenethon eypotrogers@127 .0.0.1...
      "Robert Dufour" <bdufour@sgiims .comwrote in
      news:e9cEfIPCHH A.5012@TK2MSFTN GP06.phx.gbl:
      >
      >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("MyK ey") - it'll pick it up from Project1's
      app.config.

      Comment

      • Robert Dufour

        #4
        Re: How to get app.config info from code in a referenced class

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

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

        Bob

        "Spam Catcher" <spamhoneypot@r ogers.comwrote in message
        news:Xns987C9B4 6147C3usenethon eypotrogers@127 .0.0.1...
        "Robert Dufour" <bdufour@sgiims .comwrote in
        news:e9cEfIPCHH A.5012@TK2MSFTN GP06.phx.gbl:
        >
        >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("MyK ey") - it'll pick it up from Project1's
        app.config.

        Comment

        • Spam Catcher

          #5
          Re: How to get app.config info from code in a referenced class

          "Robert Dufour" <bdufour@sgiims .comwrote in news:#Y9IKePCHH A.3836
          @TK2MSFTNGP02.p hx.gbl:
          Hi, when I try that
          Dim strTemp As String = AppSettings("my setting")
          >
          I get the wavy blue underlines and "appsetting s is a type and cannot be
          used as an expression".
          You need to import the ConfigurationSe ttings class (System.Configu ration)
          or the new Configuration Manager class (you need to reference the
          System.Configur ation DLL).

          Comment

          • Robert Dufour

            #6
            Re: How to get app.config info from code in a referenced class

            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.Get String(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

            <Spam Catcher" <spamhoneypot@r ogers.comwrote in message
            news:Xns987CA5F 26C168usenethon eypotrogers@127 .0.0.1...
            "Robert Dufour" <bdufour@sgiims .comwrote in news:#Y9IKePCHH A.3836
            @TK2MSFTNGP02.p hx.gbl:
            >
            >Hi, when I try that
            >Dim strTemp As String = AppSettings("my setting")
            >>
            > I get the wavy blue underlines and "appsetting s is a type and cannot be
            >used as an expression".
            >
            You need to import the ConfigurationSe ttings class (System.Configu ration)
            or the new Configuration Manager class (you need to reference the
            System.Configur ation DLL).

            Comment

            • Steve Long

              #7
              Re: How to get app.config info from code in a referenced class

              How 'bout the case of the variable. XML is case sensitive.

              S

              "Robert Dufour" <bdufour@sgiims .comwrote in message
              news:%23Fvdv9PC HHA.3540@TK2MSF TNGP03.phx.gbl. ..
              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.Get String(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
              >
              <Spam Catcher" <spamhoneypot@r ogers.comwrote in message
              news:Xns987CA5F 26C168usenethon eypotrogers@127 .0.0.1...
              >
              >"Robert Dufour" <bdufour@sgiims .comwrote in news:#Y9IKePCHH A.3836
              >@TK2MSFTNGP02. phx.gbl:
              >>
              >>Hi, when I try that
              >>Dim strTemp As String = AppSettings("my setting")
              >>>
              >> I get the wavy blue underlines and "appsetting s is a type and cannot be
              >>used as an expression".
              >>
              >You need to import the ConfigurationSe ttings class (System.Configu ration)
              >or the new Configuration Manager class (you need to reference the
              >System.Configu ration DLL).
              >
              >

              Comment

              • Robert Dufour

                #8
                Re: How to get app.config info from code in a referenced class

                Yes I know, I double checked, case of the variable is OK.
                Bob
                "Steve Long" <Steve_Noneya@N oSpam.comwrote in message
                news:uKEpPOQCHH A.4464@TK2MSFTN GP06.phx.gbl...
                How 'bout the case of the variable. XML is case sensitive.
                >
                S
                >
                "Robert Dufour" <bdufour@sgiims .comwrote in message
                news:%23Fvdv9PC HHA.3540@TK2MSF TNGP03.phx.gbl. ..
                >Thanks for your help. Still having a problem though. I referenced the new
                >configuratio n 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.Get String(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
                >>
                ><Spam Catcher" <spamhoneypot@r ogers.comwrote in message
                >news:Xns987CA5 F26C168usenetho neypotrogers@12 7.0.0.1...
                >>
                >>"Robert Dufour" <bdufour@sgiims .comwrote in news:#Y9IKePCHH A.3836
                >>@TK2MSFTNGP02 .phx.gbl:
                >>>
                >>>Hi, when I try that
                >>>Dim strTemp As String = AppSettings("my setting")
                >>>>
                >>> I get the wavy blue underlines and "appsetting s is a type and cannot
                >>>be
                >>>used as an expression".
                >>>
                >>You need to import the ConfigurationSe ttings class
                >>(System.Confi guration)
                >>or the new Configuration Manager class (you need to reference the
                >>System.Config uration DLL).
                >>
                >>
                >
                >

                Comment

                • Robert Dufour

                  #9
                  Re: How to get app.config info from code in a referenced class

                  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.Configur ation.Configura tionManager

                  Public Class Class1

                  Public _strTestSetting As String



                  Public Sub SetTestsetting( )

                  _strTestSetting = AppSettings.Get ("TestSettin g") -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.Configur ation

                  Public Class Form1

                  Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
                  System.EventArg s) Handles Button1.Click

                  Dim mycls As New clsTestconfig.C lass1

                  mycls.SetTestse tting()

                  Label1.Text = mycls._strTestS etting

                  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 ("TestSettin g")

                  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

                  "Steve Long" <Steve_Noneya@N oSpam.comwrote in message
                  news:uKEpPOQCHH A.4464@TK2MSFTN GP06.phx.gbl...
                  How 'bout the case of the variable. XML is case sensitive.
                  >
                  S
                  >
                  "Robert Dufour" <bdufour@sgiims .comwrote in message
                  news:%23Fvdv9PC HHA.3540@TK2MSF TNGP03.phx.gbl. ..
                  >Thanks for your help. Still having a problem though. I referenced the new
                  >configuratio n 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.Get String(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
                  >>
                  ><Spam Catcher" <spamhoneypot@r ogers.comwrote in message
                  >news:Xns987CA5 F26C168usenetho neypotrogers@12 7.0.0.1...
                  >>
                  >>"Robert Dufour" <bdufour@sgiims .comwrote in news:#Y9IKePCHH A.3836
                  >>@TK2MSFTNGP02 .phx.gbl:
                  >>>
                  >>>Hi, when I try that
                  >>>Dim strTemp As String = AppSettings("my setting")
                  >>>>
                  >>> I get the wavy blue underlines and "appsetting s is a type and cannot
                  >>>be
                  >>>used as an expression".
                  >>>
                  >>You need to import the ConfigurationSe ttings class
                  >>(System.Confi guration)
                  >>or the new Configuration Manager class (you need to reference the
                  >>System.Config uration DLL).
                  >>
                  >>
                  >
                  >

                  Comment

                  Working...