how compare object with string

Discussion in 'ASP .Net' started by =?Utf-8?B?YzY3NjIyOA==?=, Feb 14, 2006.

  1. Hi all,
    I have the code: If Session("Plan") = "Plan I" Then...
    I was just aware of that the Session("Plan") is an object, with String value
    "Plan I", but I don't know how to compare them and it never equals. I have
    tried to use
    Session("Plan").GetType.ToString.CompareTo("Plan I"), it always get 1 even
    the value for Session("Plan") is "Plan I". Can you help?
    thank you.

    --
    Betty
     
    =?Utf-8?B?YzY3NjIyOA==?=, Feb 14, 2006
    #1
    1. Advertising

  2. I think this syntax will work better for you:

    IF Session("Plan").ToString() = "Plan I" THEN...

    --
    I hope this helps,
    Steve C. Orr
    MCSD, MVP
    http://Steve.Orr.net



    "c676228" wrote:

    > Hi all,
    > I have the code: If Session("Plan") = "Plan I" Then...
    > I was just aware of that the Session("Plan") is an object, with String value
    > "Plan I", but I don't know how to compare them and it never equals. I have
    > tried to use
    > Session("Plan").GetType.ToString.CompareTo("Plan I"), it always get 1 even
    > the value for Session("Plan") is "Plan I". Can you help?
    > thank you.
    >
    > --
    > Betty
     
    =?Utf-8?B?U3RldmUgQy4gT3Jy?=, Feb 14, 2006
    #2
    1. Advertising

  3. Steve,
    Thank you. I just tried.
    Session("Plan").ToString() give me value "Plan I" string,
    but when execute the following
    IF Session("Plan").ToString() = "Plan I" THEN
    Code 1

    ELSE
    Code 2
    END IF
    It doesn't go to code 1, it goes to code 2. I cannot believe my eyes.
    Am I daydreaming?

    --
    Betty


    "Steve C. Orr" wrote:

    > I think this syntax will work better for you:
    >
    > IF Session("Plan").ToString() = "Plan I" THEN...
    >
    > --
    > I hope this helps,
    > Steve C. Orr
    > MCSD, MVP
    > http://Steve.Orr.net
    >
    >
    >
    > "c676228" wrote:
    >
    > > Hi all,
    > > I have the code: If Session("Plan") = "Plan I" Then...
    > > I was just aware of that the Session("Plan") is an object, with String value
    > > "Plan I", but I don't know how to compare them and it never equals. I have
    > > tried to use
    > > Session("Plan").GetType.ToString.CompareTo("Plan I"), it always get 1 even
    > > the value for Session("Plan") is "Plan I". Can you help?
    > > thank you.
    > >
    > > --
    > > Betty
     
    =?Utf-8?B?YzY3NjIyOA==?=, Feb 14, 2006
    #3
  4. That sounds pretty strange indeed. Sounds like some serious debugging is
    needed. Perhaps the value has a hidden character in it such as Chr(0) or
    something...

    --
    I hope this helps,
    Steve C. Orr
    MCSD, MVP
    http://Steve.Orr.net



    "c676228" wrote:

    > Steve,
    > Thank you. I just tried.
    > Session("Plan").ToString() give me value "Plan I" string,
    > but when execute the following
    > IF Session("Plan").ToString() = "Plan I" THEN
    > Code 1
    >
    > ELSE
    > Code 2
    > END IF
    > It doesn't go to code 1, it goes to code 2. I cannot believe my eyes.
    > Am I daydreaming?
    >
    > --
    > Betty
    >
    >
    > "Steve C. Orr" wrote:
    >
    > > I think this syntax will work better for you:
    > >
    > > IF Session("Plan").ToString() = "Plan I" THEN...
    > >
    > > --
    > > I hope this helps,
    > > Steve C. Orr
    > > MCSD, MVP
    > > http://Steve.Orr.net
    > >
    > >
    > >
    > > "c676228" wrote:
    > >
    > > > Hi all,
    > > > I have the code: If Session("Plan") = "Plan I" Then...
    > > > I was just aware of that the Session("Plan") is an object, with String value
    > > > "Plan I", but I don't know how to compare them and it never equals. I have
    > > > tried to use
    > > > Session("Plan").GetType.ToString.CompareTo("Plan I"), it always get 1 even
    > > > the value for Session("Plan") is "Plan I". Can you help?
    > > > thank you.
    > > >
    > > > --
    > > > Betty
     
    =?Utf-8?B?U3RldmUgQy4gT3Jy?=, Feb 14, 2006
    #4
  5. Hi Betty,

    If you're sure that the data stored in that session entry is of string
    type. Just explicitly use CType to cast the session entry to string object
    and check its value. like:

    Dim str As String
    str = CType(Session("Test"), String)

    Regards,

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)
     
    Steven Cheng[MSFT], Feb 14, 2006
    #5
  6. Agree...this should be easy to figure out if you either debug the code or
    simply Response.Write or Trace.Warn out a couple values.

    Also, on top of CType() and ToString(), I'd personally use
    Cstr(Session("Plan"))...one isn't hands down better than the other :)

    You should also make sure Option Strict is turned on in your project
    settings (it might already be, there's nothing in your code that indicates
    it isn't).

    Karl

    --
    http://www.openmymind.net/



    "Steve C. Orr" <> wrote in message
    news:...
    > That sounds pretty strange indeed. Sounds like some serious debugging is
    > needed. Perhaps the value has a hidden character in it such as Chr(0) or
    > something...
    >
    > --
    > I hope this helps,
    > Steve C. Orr
    > MCSD, MVP
    > http://Steve.Orr.net
    >
    >
    >
    > "c676228" wrote:
    >
    >> Steve,
    >> Thank you. I just tried.
    >> Session("Plan").ToString() give me value "Plan I" string,
    >> but when execute the following
    >> IF Session("Plan").ToString() = "Plan I" THEN
    >> Code 1
    >>
    >> ELSE
    >> Code 2
    >> END IF
    >> It doesn't go to code 1, it goes to code 2. I cannot believe my eyes.
    >> Am I daydreaming?
    >>
    >> --
    >> Betty
    >>
    >>
    >> "Steve C. Orr" wrote:
    >>
    >> > I think this syntax will work better for you:
    >> >
    >> > IF Session("Plan").ToString() = "Plan I" THEN...
    >> >
    >> > --
    >> > I hope this helps,
    >> > Steve C. Orr
    >> > MCSD, MVP
    >> > http://Steve.Orr.net
    >> >
    >> >
    >> >
    >> > "c676228" wrote:
    >> >
    >> > > Hi all,
    >> > > I have the code: If Session("Plan") = "Plan I" Then...
    >> > > I was just aware of that the Session("Plan") is an object, with
    >> > > String value
    >> > > "Plan I", but I don't know how to compare them and it never equals. I
    >> > > have
    >> > > tried to use
    >> > > Session("Plan").GetType.ToString.CompareTo("Plan I"), it always get 1
    >> > > even
    >> > > the value for Session("Plan") is "Plan I". Can you help?
    >> > > thank you.
    >> > >
    >> > > --
    >> > > Betty
     
    Karl Seguin [MVP], Feb 14, 2006
    #6
  7. Hi all,
    Thanks for your help.
    I used Steve Cheng's method and Steve Orr's comment is right. It is because
    of unseen character problem. Also the Option Strict on does give me some
    ideas why sometimes I have problem with my code. I willpost in another
    message.
    Dim plan As String
    plan = CType(Session("Plan"), String)
    When I moved mouse over the plan variable, if gives me the chance to find
    out(you can not see in the watch window, or through reponse.write) that
    plan="Plan I
    ". It seems not only it contains empty space, it also
    contain a line feeder character. it came to my mind that I set up the
    control:
    <td><asp:dropdownlist id="PlanList1" runat="server">
    <asp:ListItem>Choose</asp:ListItem>
    <asp:ListItem>Plan I
    </asp:ListItem>
    <asp:ListItem>Plan II
    </asp:ListItem>
    </asp:dropdownlist></td>
    Later on I changed without the newline in the control, it fixed the problem.
    It didn't make me feel so crazy anymore. Look like as follow.
    td><asp:dropdownlist id="PlanList1" runat="server">
    <asp:ListItem>Choose</asp:ListItem>
    <asp:ListItem>Plan I</asp:ListItem>
    <asp:ListItem>Plan II</asp:ListItem>
    </asp:dropdownlist></td>

    I never know it will matter, what people usually do in this code without
    change the .aspx page control set up?
    --
    Betty


    "Karl Seguin [MVP]" wrote:

    > Agree...this should be easy to figure out if you either debug the code or
    > simply Response.Write or Trace.Warn out a couple values.
    >
    > Also, on top of CType() and ToString(), I'd personally use
    > Cstr(Session("Plan"))...one isn't hands down better than the other :)
    >
    > You should also make sure Option Strict is turned on in your project
    > settings (it might already be, there's nothing in your code that indicates
    > it isn't).
    >
    > Karl
    >
    > --
    > http://www.openmymind.net/
    >
    >
    >
    > "Steve C. Orr" <> wrote in message
    > news:...
    > > That sounds pretty strange indeed. Sounds like some serious debugging is
    > > needed. Perhaps the value has a hidden character in it such as Chr(0) or
    > > something...
    > >
    > > --
    > > I hope this helps,
    > > Steve C. Orr
    > > MCSD, MVP
    > > http://Steve.Orr.net
    > >
    > >
    > >
    > > "c676228" wrote:
    > >
    > >> Steve,
    > >> Thank you. I just tried.
    > >> Session("Plan").ToString() give me value "Plan I" string,
    > >> but when execute the following
    > >> IF Session("Plan").ToString() = "Plan I" THEN
    > >> Code 1
    > >>
    > >> ELSE
    > >> Code 2
    > >> END IF
    > >> It doesn't go to code 1, it goes to code 2. I cannot believe my eyes.
    > >> Am I daydreaming?
    > >>
    > >> --
    > >> Betty
    > >>
    > >>
    > >> "Steve C. Orr" wrote:
    > >>
    > >> > I think this syntax will work better for you:
    > >> >
    > >> > IF Session("Plan").ToString() = "Plan I" THEN...
    > >> >
    > >> > --
    > >> > I hope this helps,
    > >> > Steve C. Orr
    > >> > MCSD, MVP
    > >> > http://Steve.Orr.net
    > >> >
    > >> >
    > >> >
    > >> > "c676228" wrote:
    > >> >
    > >> > > Hi all,
    > >> > > I have the code: If Session("Plan") = "Plan I" Then...
    > >> > > I was just aware of that the Session("Plan") is an object, with
    > >> > > String value
    > >> > > "Plan I", but I don't know how to compare them and it never equals. I
    > >> > > have
    > >> > > tried to use
    > >> > > Session("Plan").GetType.ToString.CompareTo("Plan I"), it always get 1
    > >> > > even
    > >> > > the value for Session("Plan") is "Plan I". Can you help?
    > >> > > thank you.
    > >> > >
    > >> > > --
    > >> > > Betty

    >
    >
    >
     
    =?Utf-8?B?YzY3NjIyOA==?=, Feb 14, 2006
    #7
  8. Glad that you've fixed the problem.

    Just be careful when input such string items in aspx, and if you're
    inputing them in VS IDE's design-time interface I think it can help avoid
    such problem a bit. Also, sometimes we use databinding to programmatically
    populate dropdownlost :)

    Regards,

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)
     
    Steven Cheng[MSFT], Feb 15, 2006
    #8
    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. Replies:
    10
    Views:
    800
  2. Replies:
    10
    Views:
    797
    Roland Pibinger
    Jan 24, 2007
  3. ankur
    Replies:
    1
    Views:
    12,969
    Jan =?UTF-8?B?VGhvbcOk?=
    Aug 27, 2007
  4. Rakesh
    Replies:
    10
    Views:
    12,230
    Mike Schilling
    Apr 8, 2008
  5. Replies:
    2
    Views:
    465
Loading...

Share This Page