Make component aware of settings for target app?

  • Thread starter Thread starter teslar91
  • Start date Start date
T

teslar91

I have a class library with a custom component (MyComponent).

MyComponent requires several values to function properly. These will
be different for each app it's used in; but for each app, these values
will be the same for each instance of MyComponent.

Is there any way I can define these values once within the app, and
have the component be able to retrieve those values, both at design
and run time?

What would be ideal is if the component could call a function within
the app that returns the values, but I'm open to other methods.
 
MyComponent requires several values to function properly. These will
be different for each app it's used in; but for each app, these values
will be the same for each instance of MyComponent.

I would put the stuff in the Web.config or App.config. Since the component
will be executing within the same process space as the application, the
component will be able to access it like this:

Import System.Configuration

Public Class Thing

Public Sub DoStuff()

Dim setting As String =
ConfigurationSettings.AppSettings("SomeKeyName")

End Sub

End Class
--

Peace & happy computing,

Mike Labosh, MCSD MCT
Owner, vbSensei.Com

"Escriba coda ergo sum." -- vbSensei
 
I would put the stuff in the Web.config or App.config. Since the component
will be executing within the same process space as the application, the
component will be able to access it like this:

Import System.Configuration

Public Class Thing

Public Sub DoStuff()

Dim setting As String =
ConfigurationSettings.AppSettings("SomeKeyName")

End Sub

End Class


This works at run time. But at design time it returns only blank
strings. Am I doing something wrong?

I've added this line to MyComponent's New event:

MsgBox(ConfigurationSettings.AppSettings("Setting1"))

And this is my app.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
</configSections>
<appSettings>
<add key="Setting1" value="Test" />
</appSettings>
<<< snip other sections >>>
</configuration>
 
This works at run time. But at design time it returns only blank
strings. Am I doing something wrong?

No, you're not doing anything wrong. I missed that part about design time.

Could you possibly skate by with a default constructor that assigns values
that the designer can use?
--

Peace & happy computing,

Mike Labosh, MCSD MCT
Owner, vbSensei.Com

"Escriba coda ergo sum." -- vbSensei
 
No, you're not doing anything wrong. I missed that part about design time.
Could you possibly skate by with a default constructor that assigns values
that the designer can use?

Yes, that's the way I'll have to do it if nothing more elegant turns
up.

Thanks for the help. I noticed that Winforms data components read the
<connectionStrings> from app.config somehow, at design-time, so maybe
there's still a way to make your suggestion work.
 
Back
Top