how compare object with string

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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.
 
I think this syntax will work better for you:

IF Session("Plan").ToString() = "Plan I" THEN...
 
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?
 
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
 
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.)
 
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
 
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?
 
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.)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top