Sunday, December 25, 2005 - Posts
.NET 2.0 introduces XML comments to VB.NET 2.0. An XML comment starts with ''' (3 single quotes). While this is new to VB.NET 2.0, XML comments have been a supported feature of C# since version 1.0. On a side note, C# XML comments start with ///.
'''
''' I'm a VB.NET XML comment
'''
One of the key features that I like most in Visual Studio 2005 is the built-in support for refactoring. This is how easy it is to extract a method. Visual Studio takes care of all the "dirty" work.
Highlight and right-click the code you want to extract to a method and select
Refactor
>
Extract Method
.
Enter the name you want to assign the extracted method.
Visual Studio extracts the method and makes all the necessary name changes, including method signatures (if applicable).
For some reason I'm not able to access public properties that I declare in a MasterPage, even though it is supposed to work. So, I discovered a way around it was to use FindControl. The example below demonstrates accessing a Literal control with an ID of litText in a MasterPage file.
VB.NET
Dim
lit
As
Literal
lit =
Me
.Master.FindControl(
"litText"
)
C#
Literal
lit;
lit = (
Literal
)
this
.Master.FindControl(
"litText"
);
Need to access an HttpContext object in a class, but can't because the class doesn't inherit System.Web.UI.Page? Here is a simple helper class that will allow you to access some of the most common HttpContext objects.
C#
using
System;
using
System.Web;
using
System.Web.Caching;
using
System.Web.SessionState;
///
///
Encapsulates all HTTP-specific information about an individual HTTP request.
///
public
class
HttpContextHelper
{
///
///
Gets the System.Web.HttpApplicationState object for the current HTTP request.
///
public
static
HttpApplicationState
CurrentApplication
{
get
{
return
HttpContext
.Current.Application; }
}
///
///
Gets the System.Web.Caching.Cache object for the current HTTP request.
///
public
static
Cache
CurrentCache
{
get
{
return
HttpContext
.Current.Cache; }
}
///
///
Gets the System.Web.HttpRequest object for the current HTTP request.
///
public
static
HttpRequest
CurrentRequest
{
get
{
return
HttpContext
.Current.Request; }
}
///
///
Gets the System.Web.HttpResponse object for the current HTTP response.
///
public
static
HttpResponse
CurrentResponse
{
get
{
return
HttpContext
.Current.Response; }
}
///
///
Gets the System.Web.HttpServerUtility object that provides methods used in
///
processing Web requests.
///
public
static
HttpServerUtility
CurrentServer
{
get
{
return
HttpContext
.Current.Server; }
}
///
///
Gets the System.Web.SessionState.HttpSessionState object for the current
///
HTTP request.
///
public
static
HttpSessionState
CurrentSession
{
get
{
return
HttpContext
.Current.Session; }
}
}
VB.NET
Imports
System
Imports
System.Web
Imports
System.Web.Caching
Imports
System.Web.SessionState
'''
''' Encapsulates all HTTP-specific information about an individual HTTP request.
'''
Public
Class
HttpContextHelper
'''
''' Gets the System.Web.HttpApplicationState object for the current HTTP request.
'''
Public
Shared
ReadOnly
Property
CurrentApplication()
As
HttpApplicationState
Get
Return
HttpContext.Current.Application
End
Get
End
Property
'''
''' Gets the System.Web.Caching.Cache object for the current HTTP request.
'''
Public
Shared
ReadOnly
Property
CurrentCache()
As
Cache
Get
Return
HttpContext.Current.Cache
End
Get
End
Property
'''
''' Gets the System.Web.HttpRequest object for the current HTTP request.
'''
Public
Shared
ReadOnly
Property
CurrentRequest()
As
HttpRequest
Get
Return
HttpContext.Current.Request
End
Get
End
Property
'''
''' Gets the System.Web.HttpResponse object for the current HTTP response.
'''
Public
Shared
ReadOnly
Property
CurrentResponse()
As
HttpResponse
Get
Return
HttpContext.Current.Response
End
Get
End
Property
'''
''' Gets the System.Web.HttpServerUtility object that provides methods used in
'''
processing Web requests.
'''
Public
Shared
ReadOnly
Property
CurrentServer()
As
HttpServerUtility
Get
Return
HttpContext.Current.Server
End
Get
End
Property
'''
''' Gets the System.Web.SessionState.HttpSessionState object for the current
'''
HTTP request.
'''
Public
Shared
ReadOnly
Property
CurrentSession()
As
HttpSessionState
Get
Return
HttpContext.Current.Session
End
Get
End
Property
End
Class
More Posts