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

Discussion in 'ASP .Net' started by dee, Apr 14, 2005.

  1. dee

    dee Guest

    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
     
    dee, Apr 14, 2005
    #1
    1. Advertising

  2. 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...
    ======================

    "dee" <dee@home> wrote in message
    news:...
    > 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
    >
    >
     
    Juan T. Llibre, Apr 14, 2005
    #2
    1. Advertising

  3. dee

    dee Guest

    Thanks Juan :)


    "Juan T. Llibre" <> wrote in message
    news:%...
    > 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...
    > ======================
    >
    > "dee" <dee@home> wrote in message
    > news:...
    >> 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
    >>
    >>

    >
    >
     
    dee, Apr 14, 2005
    #3
  4. dee

    Hans Kesting Guest

    Juan T. Llibre wrote:
    > 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...

    --
    Hans Kesting
     
    Hans Kesting, Apr 15, 2005
    #4
  5. 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...
    ======================

    "Hans Kesting" <> wrote in message
    news:...
    > Juan T. Llibre wrote:
    >> 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...
    >
    > --
    > Hans Kesting
     
    Juan T. Llibre, Apr 15, 2005
    #5
  6. dee

    Joseph Byrns Guest

    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


    "Juan T. Llibre" <> wrote in message
    news:...
    > 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...
    > ======================
    >
    > "Hans Kesting" <> wrote in message
    > news:...
    >> Juan T. Llibre wrote:
    >>> 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...
    >>
    >> --
    >> Hans Kesting

    >
    >
     
    Joseph Byrns, Apr 15, 2005
    #6
  7. 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...
    ======================

    "Joseph Byrns" <> wrote in message
    news:...
    > 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



    > "Juan T. Llibre" <> wrote in message
    > news:...
    >> 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...
    >> ======================
    >>
    >> "Hans Kesting" <> wrote in message
    >> news:...
    >>> Juan T. Llibre wrote:
    >>>> 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...
    >>>
    >>> --
    >>> Hans Kesting

    >>
    >>

    >
    >
     
    Juan T. Llibre, Apr 15, 2005
    #7
  8. dee

    Joseph Byrns Guest

    Then it's a string, but there are no quotes in the example provided above.

    "Juan T. Llibre" <> wrote in message
    news:...
    > 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...
    > ======================
    >
    > "Joseph Byrns" <> wrote in message
    > news:...
    >> 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

    >
    >
    >> "Juan T. Llibre" <> wrote in message
    >> news:...
    >>> 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...
    >>> ======================
    >>>
    >>> "Hans Kesting" <> wrote in message
    >>> news:...
    >>>> Juan T. Llibre wrote:
    >>>>> 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...
    >>>>
    >>>> --
    >>>> Hans Kesting
    >>>
    >>>

    >>
    >>

    >
    >
     
    Joseph Byrns, Apr 15, 2005
    #8
  9. 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...
    ======================

    "Joseph Byrns" <> wrote in message
    news:...
    > Then it's a string, but there are no quotes in the example provided above.
    >
    > "Juan T. Llibre" <> wrote in message
    > news:...
    >> 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...
    >> ======================
    >>
    >> "Joseph Byrns" <> wrote in message
    >> news:...
    >>> 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

    >>
    >>
    >>> "Juan T. Llibre" <> wrote in message
    >>> news:...
    >>>> 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...
    >>>> ======================
    >>>>
    >>>> "Hans Kesting" <> wrote in message
    >>>> news:...
    >>>>> Juan T. Llibre wrote:
    >>>>>> 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...
    >>>>>
    >>>>> --
    >>>>> Hans Kesting
    >>>>
    >>>>
    >>>
    >>>

    >>
    >>

    >
    >
     
    Juan T. Llibre, Apr 15, 2005
    #9
  10. dee

    Joseph Byrns Guest

    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




    "Juan T. Llibre" <> wrote in message
    news:...
    > 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...
    > ======================
    >
    > "Joseph Byrns" <> wrote in message
    > news:...
    >> Then it's a string, but there are no quotes in the example provided
    >> above.
    >>
    >> "Juan T. Llibre" <> wrote in message
    >> news:...
    >>> 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...
    >>> ======================
    >>>
    >>> "Joseph Byrns" <> wrote in message
    >>> news:...
    >>>> 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
    >>>
    >>>
    >>>> "Juan T. Llibre" <> wrote in message
    >>>> news:...
    >>>>> 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...
    >>>>> ======================
    >>>>>
    >>>>> "Hans Kesting" <> wrote in message
    >>>>> news:...
    >>>>>> Juan T. Llibre wrote:
    >>>>>>> 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...
    >>>>>>
    >>>>>> --
    >>>>>> Hans Kesting
    >>>>>
    >>>>>
    >>>>
    >>>>
    >>>
    >>>

    >>
    >>

    >
    >
     
    Joseph Byrns, Apr 15, 2005
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ajamrozek
    Replies:
    1
    Views:
    500
    ajamrozek
    Sep 22, 2005
  2. Anand
    Replies:
    2
    Views:
    915
    Anand
    Sep 11, 2003
  3. Roger
    Replies:
    1
    Views:
    734
    Roger
    Jan 10, 2007
  4. DC
    Replies:
    1
    Views:
    1,135
  5. Paul fpvt2
    Replies:
    1
    Views:
    200
    Aaron [SQL Server MVP]
    Nov 2, 2004
Loading...

Share This Page