?
Solved

500pts: Global variables?

Posted on 2006-04-22
16
Medium Priority
?
325 Views
Last Modified: 2008-02-26
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
0
Comment
Question by:ianinspain
  • 8
  • 7
16 Comments
 
LVL 9

Expert Comment

by:daniel_balla
ID: 16516069
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
 

Author Comment

by:ianinspain
ID: 16516133
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
 
LVL 6

Expert Comment

by:nehaya
ID: 16516203
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 6

Expert Comment

by:nehaya
ID: 16516207
ByTheWay, you can use the application_start etc to load your data and then close all files when ending..
0
 

Author Comment

by:ianinspain
ID: 16516250
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
 

Author Comment

by:ianinspain
ID: 16516312
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
 

Author Comment

by:ianinspain
ID: 16516350
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
 
LVL 6

Expert Comment

by:nehaya
ID: 16516928
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
 

Author Comment

by:ianinspain
ID: 16516937
Thanks

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

ian
0
 
LVL 6

Expert Comment

by:nehaya
ID: 16516978
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
 
LVL 6

Expert Comment

by:nehaya
ID: 16516987
Make sure u put the public variables (BEFORE) the ApplicaionStart method.
0
 
LVL 6

Expert Comment

by:nehaya
ID: 16516994
see this for more choices and usage and understanding:
http://www.eggheadcafe.com/articles/20030211.asp
0
 

Author Comment

by:ianinspain
ID: 16516996
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
 

Author Comment

by:ianinspain
ID: 16517001
note even using this

HttpContext.ApplicationInstance.MyString

0
 
LVL 6

Accepted Solution

by:
nehaya earned 2000 total points
ID: 16517078
-> 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
 

Author Comment

by:ianinspain
ID: 16517849
thanks for sticking by on this... i am alot more knowledgeable now... thanks again! it was in currentcontext

ian
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month16 days, 16 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question