Click to See Complete Forum and Search --> : Share information between classes
EEKstream
August 1st, 2006, 12:47 AM
i have a couple of asp pages that needs some common information (for instace a username to the database, the version of the site and so on). Which is the best/simpliest way to store them? Because I cannot put a static variable inside one of my asp classes and reach it from the others
Shuja Ali
August 1st, 2006, 02:18 AM
How about using session variables?
EEKstream
August 1st, 2006, 03:09 AM
It's nothing I want to set in "run" time or anything that will depend on the current session/user or anything like that, just constants. For instance, I need to connect to a database in all classes, therefore I would like to store the host IP somewhere and use in my pages. (I only want to change this at compile time). Or can I just create a new ordinary class within the project and use some public constants?
Edit: I tried the latter and it works fine.
Shuja Ali
August 1st, 2006, 03:30 AM
It's nothing I want to set in "run" time or anything that will depend on the current session/user or anything like that, just constants. For instance, I need to connect to a database in all classes, therefore I would like to store the host IP somewhere and use in my pages. (I only want to change this at compile time). Or can I just create a new ordinary class within the project and use some public constants?
Edit: I tried the latter and it works fine.
Or just add them to the web.config file.
EEKstream
August 1st, 2006, 04:41 AM
How do I write them in the config file? I would like to have a string...
Edit: I've been able to add values now, like
<add key="XXX" value="yadayada" />
But how do I reach them from within the other classes?
Shuja Ali
August 1st, 2006, 05:52 AM
You will have to use System.Configuration namespace to read the sections in App.config.
Take a look at the code provided here
http://www.willasrari.com/blog/categories.aspx?categoryID=2
EEKstream
August 1st, 2006, 06:53 AM
Thanks!
Just one more question :) Will the config-values be readed when the pages are executed or is it at compile time? (would be nice to be able to change the values without having Visual Studio on the web server)
Shuja Ali
August 1st, 2006, 06:59 AM
Configuration files are simple XML files which can be modified anytime and the same change will be reflected in your code too without compiling the code again.
The Configuration settings are read when the pages are executed and not at the compile time, thats why I said you can change the values in the config files any time and the same will be reflected in the pages too without recompiling.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.