views:

21

answers:

2

.

Session("UserName") = "Sally"  
Dim userName As String = Session("UserName")

Do i need to convert the session variable to string if i wanna follow "good coding practices"?

Ex:

Session("UserName") = "Sally"  
Dim userName As String = Convert.ToString(Session("UserName"))
A: 

Yes as a good practice and if you want to assign to a new variable:

Dim userName As String = Session("UserName")

Otherwise you can use it directly:

Print Session("UserName")

Note that value "Sally" (wrapped in quotes) is a string.

Sarfraz
I was assigning the sessionvariable to a new string variable in my question?! And yes "Sally" is a string and it should be a string when assigning it to the session variable.
EasyDot
+1  A: 

IMO, you should be using

Option Strict On
Option Explicit On

at all times, it makes the compiler yell at you when you cast implicitly or use undeclared identifiers.

tdammers
Yes i have the strict and explicit mode on all the time! :)
EasyDot
If that's the case, then the compiler will warn you when you assign to a string variable from an object. String is a reference type though, so you still have to check for null / Nothing yourself.
tdammers