Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / .NET SDK / January 2004

Tip: Looking for answers? Try searching our database.

Super strange problem with strings and reflection

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Prashant Deva - 26 Dec 2003 19:08 GMT
I am using reflection to initialize a string to a value.

After initializing, i see the value of the string in my debugger as-

type="breakpoint"

but strangely, when i put code like this

if(type=="breakpoint")
{
   //do whatever
}

it FAILS!!!!

i used NUnit to perform a test and it says-

----------------------------------------------------------------------------
----------------
Debugger.Commands.InfoStructs.Test.BreakpointInfoTest.Test :
String lengths differ.  Expected length=10, but was length=11.
Strings differ at index 10.

expected:<"breakpoint">
 but was:<"breakpoint(a square box here, it cant be pasted in outlook)">
----------------------------------------------------------------------------
-----------------------

What exactly is happening?
when i do the type.Length in the debugger watch window it returns 10.

But strangely in the debugger if i manually do
type="breakpoint"
and then
type=="breakpoint"
it returns true?

i am using .net framework 1.0.

Any help will be fully appreciated.

PRASHANT
Ken Pinard - 26 Dec 2003 23:28 GMT
Have you parsed the last character out of the string and then seen what the
ASC value is?

It would be helpful here.

If  zero(0) then it is a string terminator and maybe it needs to be parsed
out.

HTHs
Ken

> I am using reflection to initialize a string to a value.
>
[quoted text clipped - 38 lines]
>
> PRASHANT
Prashant Deva - 30 Dec 2003 08:40 GMT
 Ok, i have found out that the problem is not due to reflection but due to
this-

if i make a char array which is even 1 more than the length of string to be
stored-
char[] a = new char[len+1];
and store chars in it till len and then make a string out of the array-
string s =a.ToString();
the returned string has "\0" in it.
So if the string  you copied is "Hello" but if you compare it with "Hello"
it will fail-
if(s=="Hello") //this FAILS!
To make the test pass you have to do include the "\0" in the string, like
this-
if(s=="Hello\0")//this PASSES!

but strangely if you check for length it will return it to you without
considering the null.
int len = s.Length;// len is equal to 5. NOT 6

This seems to be a bug in .NET 1.0.

PRASHANT

> Have you parsed the last character out of the string and then seen what the
> ASC value is?
[quoted text clipped - 52 lines]
> >
> > PRASHANT
Ken Pinard - 05 Jan 2004 02:39 GMT
Actually, this has been a feature of "C" since the beginning. If the length
was greater than 1 more than the length. It would have any previously
assigned character in the last character.

You need to do your own checking.

Ken

>   Ok, i have found out that the problem is not due to reflection but due to
> this-
[quoted text clipped - 78 lines]
> > >
> > > PRASHANT

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.