500pts: Global variables?

Hi there,

Can anyone suggest the best method of using a global variable, now i know these are not technically supported but my scenerio is that i have a couple of XML config files that i wish to import and store so that they will be available through the life of the application.

These config files store things like "name of website", "can use ftp" etc etc... so technically they would never change and it seems pointless loading these in on every page if i need to use them... is there a better method?

Session i can't use as they expire.... but something similar?? maybe using the global.asax file?

Any ideas or advice?

Thanks in advance

Ian
ianinspainAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

daniel_ballaCommented:
Hi
you can use Application object. it will only expire when all sessions expire, which is reasonable to re-read it from your xml configuration files. You could also use a public static variable which would be available throughout your application.
Cheers
0
ianinspainAuthor Commented:
thanks for the input... but if i use a public static variable for example in default.aspx then in my admin.aspx it won't be available will it?

of course taking a trip back to default.aspx then it will..

Maybe if i create a static class with properties and update and retrieve data that way, this could be gotton from each web page as it is static.. no instance required..

of course then there is application["test"]= "mydata"

but which is better??

I am interested to know more on your idea of using a public static variable that is available to all pages..... do you mean creating a public static variable in global.asax ?? can you do this, i though we had to use application[""].....

What do think is best way of doing this....??

p.s. yes i won't be really updating it again, just on initial app load...... thats all

thanks

ian
0
nehayaCommented:
Think about Global.asax
put public variable(e.g. public string SomeVariable ), and from your pages (e.g. from Page_Load) , use this:
ApplicationInstance.SomeVariable = VALUE;
OR
VALUE = ApplicationInstanec.SomeVariable;

Regards,
Nehaya
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

nehayaCommented:
ByTheWay, you can use the application_start etc to load your data and then close all files when ending..
0
ianinspainAuthor Commented:
Thanks for the reply... but using public or static in the global.asax causes an error.. so i had to stick with the following but then it wouldn't be public static would it??

    void Application_Start(object sender, EventArgs e)
    {
        // Code that runs on application startup
        string testme = "hello";

    }

... then i go to one of my webforms but ApplicationInstance.testme isn't available.... infact ApplicationInstance doesn't seem to exist i.e.

            Response.Write(ApplicationInstance.testme);



0
ianinspainAuthor Commented:
Ahh .. i did have one issue... i am using visual studio 2005 and global.asax doesn't have a codebehind, but a new release of web application project has been released which gives Visual studio 2005 the same type of things visual studio 2003 does... I have just removved my global.asax and created a new one and now i have code behind...

0
ianinspainAuthor Commented:
ok its working now....

in global.asax codebehind

public static string testme="hello";

and in a webpage in page_load

            Response.Write(Global.testme);

and it works, thanks very much for that...

Can you comment on weather using this way (which i like alot!) or using application object is better.... I can't understand why they would provide 2 ways! :-)

I think i read somewhere that this method that i am using now may not be thread safe i.e. application object you can lock and unlock but this you can't.... do you know of a way i can implement locking of this variable just in case i need to use it in the future i.e. thread safe?

Thanks again.

p.s. visual studio 2005 is great but it has a lot to answer for!!! but with the web application projects it introduces how we used to work in VS 2003 which is ok...

Thanks

Ian
0
nehayaCommented:
Static always suffer from threading safe problems if they didn't implemented carefully..
\\
no need for codebehined!
just write ur declaration line before:
void Application_Start(object sender, EventArgs e)
{
}
and it will work!

\\
Both ways work, but IMHO, I recomend using ApplicationInstance..

Regards,
Nehaya
0
ianinspainAuthor Commented:
Thanks

how do i use ApplicationInstance again?? in the page_load, it wasn't valid when i tried it

ian
0
nehayaCommented:
in 2005, i created new website,
added new item called : Gloabl.asax

in Global
before Application_Start, I put this line
public string MyString = "";

and in global also, in SessionStart metod, I added this
MyString = "Hello";

and in Default, in Page_Load, I wrote this:
Response.Write(ApplicationInstance.MyString);

and I run the application, and it printed: "Hello".

Regards
Nehaya
0
nehayaCommented:
Make sure u put the public variables (BEFORE) the ApplicaionStart method.
0
nehayaCommented:
see this for more choices and usage and understanding:
http://www.eggheadcafe.com/articles/20030211.asp
0
ianinspainAuthor Commented:
nope it doesn't exist here

applicationinstance isn't recognized..

p.s. quick question.. why did you only use Public String and not public static string??

Thanks fro your continued support

in global

public class Global : System.Web.HttpApplication
    {
        public static string testme="hello";
        public string MyString = "";

        protected void Application_Start(object sender, EventArgs e)
        {
            MyString = "Hello";
        }


and in page_load of form

            Response.Write(ApplicationInstance.MyString);
0
ianinspainAuthor Commented:
note even using this

HttpContext.ApplicationInstance.MyString

0
nehayaCommented:
-> p.s. quick question.. why did you only use Public String and not public static string??
because : You do not have to worry about locking when you access non-static members of the application class (form the link i gave last time)

Are you using 2005?
in your default page, try this:
HttpContext.Current.ApplicationInstance
Must be somewhere :)

P.S: Do not put:
MyString = "Hello";
in applicaation start, put it in Session_Start.

p.s my global.asax file looks like this(no code behind) : (do not copy it, i wrote it manually cause my asp.net in my other pc)

<%@ Application language="c#" %>
<script runat="server">
public string MyString;
void Application_Start(object sender, EventArgs e)
{
}

..
..
..

</script>

Regards,
Nehaya
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ianinspainAuthor Commented:
thanks for sticking by on this... i am alot more knowledgeable now... thanks again! it was in currentcontext

ian
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.