Can't get application obj to work in a code behind class

A

Andrew Fisher

Hi there.

I am finally moving over to ASP.NET as the company I work for is
updating certain systems, ,so forgive me if this quesiton has been
answered before [I can't seem to find an answer].

I am using C# for this.

As a test I have built a class that sets up a wrapper around the
SmtpMail object so that with only a couple of statements an email can
be sent [this is just a test as this replicates what we will really be
doing with business objects].

So, to save on memory etc, I'd like to load this custom email class
into an Application object - something like

pmx.EmailSender Application["emailer"] = new EmailSender();

So it can be reused wherever in the application.

I'd then like in whatever .aspx file to be able to call the object as:

Application["emailer"].SendEmail (to, from, body);

And it will do it.

I've put the first bit of code into the global.asax file and it throws
an error:

Syntax error, bad array declarator. To declare a managed array the rank
specifier precedes the variable's identifier.

But I'm not sure what this means and if I am even approaching this the
write way.

Weirdly I seem to find loads of information saying you can store
Objects in the Application object but then everyone just uses the
example Application["varname"] = "some string"; - Not really the height
of complexity!

Any help or suggestions would be very greatly appreciated.
Cheers
AndrewF
 
P

Patrice

Type Variable[rank]. is to declare an array.

It should be just :

Application["emailer"]=new EmailSender();

As a side not you could just expose this as a class static member. You could
then use directly this class rather than storing an instance in an
application variable...

Patrice
 
A

Andrew Fisher

Thanks for your help Patrice however I've now created a different
problem.

I can declare this variable now okay and that isn't throwing an error,
but now when I go to use it in my page such as:

Application["emailer"].to = "(e-mail address removed)";

It is now saying that: 'object' does not contain a definition for 'to'
- it is all spelt right etc so I know that there is a definition for
the propert "to" in this object.

Now I know what the compiler is telling me, and in your explanation
above I realise now that the Application object is just an array of
objects so I am dropping an object onto that array... but how do I now
use the properties and methods of my custom object.

I'd be quite happy if there is a better way of doing this as well so if
someone can enlighten me on this score I'd be very grateful.

AndrewF
 
P

Patrice

?

=?ISO-8859-1?Q?Anders_Nor=E5s?=

Andrew said:
Application["emailer"].to = "(e-mail address removed)";
(abridged) Now I know what the compiler is telling me, and in your explanation
above I realise now that the Application object is just an array of
objects so I am dropping an object onto that array...
The Application collection stores everything as System.Object, so you'll
have to do a type-cast to convert the EmailSender from System.Object to
EmailSender. The following examples use different techniques to do this:
// Do this:
((EmailSender)Application["emailer"]).to "(e-mail address removed)";
// ..or this..
EmailSender mySender=(EmailSender) Application["emailer"];
mySender.to="(e-mail address removed)";
// ..or this...
EmailSender mySender=Application["emailer"] as EmailSender;
mySender.to="(e-mail address removed)";

Anders Norås
http://dotnetjunkies.com/weblog/anoras/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top