Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / February 2008

Tip: Looking for answers? Try searching our database.

Reference To An Instantiating Obect

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Microsoft Newsserver - 28 Feb 2008 14:32 GMT
I am instantiating an object in an ASP.NET page. From this object I want to
be able to access Session. I dont want to change the constructor to pass the
page reference to it, and I dont want to have to add extra properties.

How can I get a reference to the page object  in which the object is
instantiated.

Cheers
Jon Skeet [C# MVP] - 28 Feb 2008 15:05 GMT
> I am instantiating an object in an ASP.NET page. From this object I want to
> be able to access Session. I dont want to change the constructor to pass the
> page reference to it, and I dont want to have to add extra properties.
>
> How can I get a reference to the page object  in which the object is
> instantiated.

Why would you believe there's a way to do this? Objects don't have the
notion of a "creator". You need to tell them about objects.

It's possible that there's some thread-static way to get to the
"current page" via HttpContext.Current.Request or something similar...
but there's no general solution.

If you want to give access to the reference, why don't you want to
change the constructor? You're really trying to add state without
adding state...

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Microsoft Newsserver - 28 Feb 2008 15:37 GMT
>> I am instantiating an object in an ASP.NET page. From this object I want
>> to
[quoted text clipped - 7 lines]
> Why would you believe there's a way to do this? Objects don't have the
> notion of a "creator". You need to tell them about objects.

Answer.
It was a question.  I was wondering if there was a way do do this using
reflection or some other method.
A simple yes or no would have sufficed.

> It's possible that there's some thread-static way to get to the
> "current page" via HttpContext.Current.Request or something similar...
> but there's no general solution.

Answer.
Actually, I had forgotten about the system.web namespace which I added
as a reference and then was able to acheive this.

> If you want to give access to the reference, why don't you want to
> change the constructor? You're really trying to add state without
> adding state...

No comment.
Jesse Houwing - 28 Feb 2008 15:09 GMT
Hello Microsoft,

> I am instantiating an object in an ASP.NET page. From this object I
> want to be able to access Session. I dont want to change the
[quoted text clipped - 3 lines]
> How can I get a reference to the page object  in which the object is
> instantiated.

You can use the HttpContext.Current static variable. You can access both
the current page and the session from there.

--
Jesse Houwing
jesse.houwing at sogeti.n
Microsoft Newsserver - 28 Feb 2008 15:38 GMT
Thanks for your help.

I added the system.web namspace and used it. I had forgotten about that
facility.

> Hello Microsoft,
>
[quoted text clipped - 12 lines]
> Jesse Houwing
> jesse.houwing at sogeti.nl
Arne Vajhøj - 29 Feb 2008 04:05 GMT
>> I am instantiating an object in an ASP.NET page. From this object I
>> want to be able to access Session. I dont want to change the
[quoted text clipped - 6 lines]
> You can use the HttpContext.Current static variable. You can access both
> the current page and the session from there.

The class will only work in web context then.

I would try and avoid that type of coupling.

Arne
Jesse Houwing - 29 Feb 2008 10:03 GMT
Hello Arne,

>>> I am instantiating an object in an ASP.NET page. From this object I
>>> want to be able to access Session. I dont want to change the
[quoted text clipped - 10 lines]
>
> I would try and avoid that type of coupling.

You could check

If (HttpContext.Current != null)
{
   // In web context
}
else
{
  // other
}

Either way, if you need the Session or elements on the page in the class,
you're coupled any way.

--
Jesse Houwing
jesse.houwing at sogeti.n
Arne Vajhøj - 29 Feb 2008 17:43 GMT
>>>> I am instantiating an object in an ASP.NET page. From this object I
>>>> want to be able to access Session. I dont want to change the
[quoted text clipped - 24 lines]
> Either way, if you need the Session or elements on the page in the
> class, you're coupled any way.

Yes.

The problem is not the answer but the question.

Arne
Jesse Houwing - 29 Feb 2008 18:04 GMT
Hello Arne,

>>>>> I am instantiating an object in an ASP.NET page. From this object
>>>>> I want to be able to access Session. I dont want to change the
[quoted text clipped - 27 lines]
>
> The problem is not the answer but the question.

Yes, but that also depends on what the class must do. If it's specialised
in formattign or chackign controls on the page, then this is a completely
logocal thing to do...

--
Jesse Houwing
jesse.houwing at sogeti.nl
Arne Vajhøj - 29 Feb 2008 18:40 GMT
>>>>>> I am instantiating an object in an ASP.NET page. From this object
>>>>>> I want to be able to access Session. I dont want to change the
[quoted text clipped - 31 lines]
> specialised in formattign or chackign controls on the page, then this is
> a completely logocal thing to do...

But making the Session or whatever being transferred as an argument
instead of grabbed via .Current would make the dependency explicit.

Arne
Cowboy (Gregory A. Beamer) - 28 Feb 2008 16:28 GMT
Yes, you can do this, as Jesse has mentioned, by working through current
context.

There is also a good reason why this, as a rule, is not a good idea. You are
tightly coupling your class (or class library) to a web application. This
does a couple of things:

1. Makes it difficult, if not impossible, to unit test using unit testing
frameworks, as you have forced a dependency that cannot easily be injected,
if at all

2. Forces a complete redesign of your application if you ever move to
another type of library (web service, smart client, etc.)

You are best advised to grab the info you need off the page before you fire
off the library, as this leaves no circular dependencies.

Signature

Gregory A. Beamer
MVP, MCP: +I, SE, SD, DBA

*************************************************

| Think outside the box!

*************************************************
>I am instantiating an object in an ASP.NET page. From this object I want to
>be able to access Session. I dont want to change the constructor to pass
[quoted text clipped - 4 lines]
>
> Cheers
Microsoft Newsserver - 29 Feb 2008 08:24 GMT
Point noted.  - Thanks

> Yes, you can do this, as Jesse has mentioned, by working through current
> context.
[quoted text clipped - 24 lines]
>>
>> Cheers

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.