Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > how compare object with string

Reply
Thread Tools

how compare object with string

 
 
=?Utf-8?B?YzY3NjIyOA==?=
Guest
Posts: n/a
 
      02-14-2006
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
 
Reply With Quote
 
 
 
 
=?Utf-8?B?U3RldmUgQy4gT3Jy?=
Guest
Posts: n/a
 
      02-14-2006
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

 
Reply With Quote
 
 
 
 
=?Utf-8?B?YzY3NjIyOA==?=
Guest
Posts: n/a
 
      02-14-2006
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

 
Reply With Quote
 
=?Utf-8?B?U3RldmUgQy4gT3Jy?=
Guest
Posts: n/a
 
      02-14-2006
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

 
Reply With Quote
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      02-14-2006
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.)

 
Reply With Quote
 
Karl Seguin [MVP]
Guest
Posts: n/a
 
      02-14-2006
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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



 
Reply With Quote
 
=?Utf-8?B?YzY3NjIyOA==?=
Guest
Posts: n/a
 
      02-14-2006
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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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

>
>
>

 
Reply With Quote
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      02-15-2006
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.)

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
java.util.Properties extending from HashMap<Object, Object> insteadof HashMap<String, String> Rakesh Java 10 04-08-2008 04:22 AM
searching an array, string compare functions, string sorting ckirchho@directmedia.de Javascript 2 10-10-2007 11:23 PM
compare string and "" string literal lovecreatesbea...@gmail.com C++ 10 01-24-2007 09:55 PM
strcmp vs. string::compare(const string &) lchian@yahoo.com C++ 10 03-03-2006 08:53 PM
Object creation - Do we really need to create a parent for a derieved object - can't the base object just point to an already created base object jon wayne C++ 9 09-22-2005 02:06 AM



Advertisments