Session Variable and Casting

  • Thread starter Thread starter Rajesh Abraham
  • Start date Start date
R

Rajesh Abraham

I am new to Asp world and I have a doubt as follows. I
have a class called objTranslator, which I declare and
initiate global.asax as follows.

Session_Start
csTranslator.objTranslator sesObjTranslator= new
objTranslator();
Session["sesTranslator"]=sesObjTranslator;
----------------
Now in WebForm

private csTranslator.objTranslator webTranslator;

In Page Load

if(Session["sesTranslator"]!=null)
webTranslator=(objTranslator)Session["sesTranslator"];

The question is why do I have to explictly cast the
session variable sesTransilator to objTransilator while it
is holding a reference to sesObjTranslator.

So actually what type is the session variable.

In VB.Net one can directly use

webTranslator=Session("sesTranslator")

What is the difference here?

Thanks,

Rajesh Abraham Chacko
 
The Session[] object is a collection of objects. That way it can hold any
type of class (since all are derived from object). By casting, you are
telling the compiler that you know exactly what type of object that should
be. VB doesn't have as advanced a pre-compile type checking system
(allowing for more 'sloppy' code).

-Noah Coad
Microsoft MVP & MCP [.NET/C#]
 
Back
Top