By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,786 Members | 1,142 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,786 IT Pros & Developers. It's quick & easy.

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

P: n/a
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

Nov 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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

--

"Andrew Fisher" <dr********@hotmail.com> a écrit dans le message de
news:11**********************@c13g2000cwb.googlegr oups.com...
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

Nov 19 '05 #2

P: n/a
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 = "em***@address.com";

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

Nov 19 '05 #3

P: n/a
You have now to "cast" the variable so that the compiler knows the expected
type.

As I said you could likely achieve the same result by creating a class with
static members (i.e. members that "belongs" to the class rahter than to an
instance). This way there is no need to store an instance then in
Application...

I suggest reading at least one time the whole C# spec :

http://msdn.microsoft.com/library/de...pSpecStart.asp
Patrice

--

"Andrew Fisher" <dr********@hotmail.com> a écrit dans le message de
news:11**********************@z14g2000cwz.googlegr oups.com...
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 = "em***@address.com";

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

Nov 19 '05 #4

P: n/a
Andrew Fisher wrote:
Application["emailer"].to = "em***@address.com";
(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 "em***@address.com";
// ..or this..
EmailSender mySender=(EmailSender) Application["emailer"];
mySender.to="em***@address.com";
// ..or this...
EmailSender mySender=Application["emailer"] as EmailSender;
mySender.to="em***@address.com";

Anders Norås
http://dotnetjunkies.com/weblog/anoras/
Nov 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.