Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

newbie question about classes

Status
Not open for further replies.

ericgeil

Programmer
Apr 9, 2009
4
US
I'm moving some of my code from ASP to ASP.net. I'm using VS.net 2003. I would like to use something like an include file to make some common functions available to all of my pages. I'm trying to do this using a class (.vb file) and adding a reference to this class in my .aspx file's code behind page.

The problem is, it looks like if I make the sub/function in my .vb file SHARED, it can't access things like Session, Response and Request. I get this build error : "Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class."

If I don't make it shared, it can access the session, response and request objects, but I can't access the sub/function from my aspx page. What am I missing here?
 
i think this applies for VB too, but use HttpContext.Current

HttpContext.Current.(Request or Response or Session).WhateversNext
 
sry, that might have been confusing...

HttpContext.Current.Request.QueryString["qs"]
or
HttpContext.Current.Response.StatusCode = "301"
or
HttpContext.Current.Session["sessionVar"].ToString()
 
That worked...thanks!

why is the HttpContext.Current. necessary? can I avoid this by using Imports or Inherits?
 
imports reference namespaces to ease declaring objects.
example
Code:
imports System.IO
Dim file as new FileInfo("path to file")
instead of
[/code]
Dim file as new System.IO.FileInfo("path to file")
[/code]

Inherits refers to inheritance in terms of OOP.
Code:
class Foo
end class

class Bar Inherits Foo
end class

HttpContext.Current is a publicly static accessor to the current http instance. Some have called this the Static Gateway Pattern.

the class you are creating wouldn't inherit the HttpContext, it would reference it. another option, instead of referencing the static assessor is to inject the HttpContext instance into your object. (switch to c#, can't write vb)
Code:
public class Foo
{
   //to be used in this class to reference context stuff
   private readonly HttpContext context;

   public Foo(HttpContext context)
   {
     this.context = context;
   }
}
there are advantages to dependency injection. while there is no advantage with HttpContext because this is one of the worst classes in the framework. (only work with asp.net running, so you cannot mock, or unit test with it.)

asp.net is an OOP language where asp is a scripted language. There will be some differences when converting from one to the other.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
I prefer to do it the way that Jason has mentioned here. He is correct that HttpContext only works when running, but you can mock it with some bit of effort.
 
but you can mock it with some bit of effort.
some? :), maybe if you're thinking adapter, but even that requires more coding than I want to think about. fortunately any MVC framework has done this work for me.

David, it's good to see another member mention mocking :) Not many devs know about mocks and/or unit testing.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Not many devs know about mocks and/or unit testing.
Sad, but very true. All of the developers that I hire, the first thing i make them learn is unit testing and mocking. It makes our jobs much easier in the long run.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top