Session("passed") = Session("passed") + 1 error

  • Thread starter Thread starter dee
  • Start date Start date
D

dee

Hi
My code complies the following line:
Session("passed") = 1
but puts wiggly error line under the second Session("passed") in the
following expression:
Session("passed") = Session("passed") + 1
Why?
Thanks
Dee.


Session("passed") = Session("passed") + 1

Session("passed") = Session("passed") + 1
 
Because Session("passed") is a string.
Try casting to Int, and then adding 1 to it.



Juan T. Llibre
ASP.NET MVP
http://asp.net.do/foros/
Foros de ASP.NET en Español
Ven, y hablemos de ASP.NET...
======================
 
Juan said:
Because Session("passed") is a string.
Try casting to Int, and then adding 1 to it.

Session("whatever") is not a string, it's an "object".
You can't cast a string to an int (you have to "parse" it),
but you can cast an object to an int (if it really *is* an int).
So your solution *does* work...
 
Hello, Hans.

While the Session object is an object ( of course ),
its *content* can be a string, as in this particular case
....where the 1 in Session("passed") = 1 is a string, not an object.

This works, for example :

Session("passed") = 1
Dim yNumber as String = Session("passed")
Dim jNumber as Integer = Int32.Parse(yNumber)
Dim wNumber as Integer = jNumber + jNumber
lblMessage.Text = wNumber.ToString()

You're right about the use of "casting", though.
That was a bit sloppy on my part.

I should have used "Convert.ToInt32" or "Parse".

Int32.Parse() is what Convert.ToInt32() calls, anyway, isn't it ?




Juan T. Llibre
ASP.NET MVP
http://asp.net.do/foros/
Foros de ASP.NET en Español
Ven, y hablemos de ASP.NET...
======================
 
Actually I think you'll find it's an int32 not a string, if you do:

Session("test") = 1

Dim a As String = Session("test").GetType.ToString



You'll see that a is System.Int32
 
Cool...

And Session("passed") = "1" 's type is ... ?

;-)

Question for you :

How does Session("passed") = 1
get converted from Int32 to String in
Dim yNumber as String = Session("passed")

Shouldn't that cause an "incorrect type" error ?



Juan T. Llibre
ASP.NET MVP
http://asp.net.do/foros/
Foros de ASP.NET en Español
Ven, y hablemos de ASP.NET...
======================
 
Then it's a string, but there are no quotes in the example provided above.

Juan T. Llibre said:
Cool...

And Session("passed") = "1" 's type is ... ?

;-)

Question for you :

How does Session("passed") = 1
get converted from Int32 to String in
Dim yNumber as String = Session("passed")

Shouldn't that cause an "incorrect type" error ?



Juan T. Llibre
ASP.NET MVP
http://asp.net.do/foros/
Foros de ASP.NET en Español
Ven, y hablemos de ASP.NET...
======================
 
re:
Then it's a string, but there are no quotes in the example provided above.

I know...

Do you have any ideas about the question I asked ?

How does Session("passed") = 1
get converted from Int32 to String in
Dim yNumber as String = Session("passed")

Shouldn't that cause an "incorrect type" error ?




Juan T. Llibre
ASP.NET MVP
http://asp.net.do/foros/
Foros de ASP.NET en Español
Ven, y hablemos de ASP.NET...
======================
 
Hmm, I think .net is just being friendly and automatically casting the int32
to a string for you. That's my guess anyway.

you get the same thing if you do:

Dim a As Int32 = 1

Dim b As String = a
 
Back
Top