|
String.Intern Comparison Issues:
This is a problem that came up when reading the
DR GUI .NET #5 and a question was posted in
microsoft.public.dotnet.languages.vb.
The original posted message is here.
Problem Solved. I need more sleep. I misunderstood the article.
After reading DR GUI .NET #5, I wanted to give String.Intern a try.
I am either having an issue understanding how the IS operator
is expected to work when comparing an Intern to a String, or a bug
exists somewhere in my code or the Framework.
What I did was set up the following tests.
1. Compare a regular DIM S as string = "Value" to an intern
2. Create an Intern and compare it to a String return value from a
function
3. Create an Intern and compare it to a object.ToString value
4. Try casting the above example to objects
5. Get the Type of an object and compare the FullName Property to an
intern
6. Convert a String to a byte array and back to a String and compare
to an intern
7. Create another Dim s as string = "Value" to compare it to the
second intern to make sure it was actually an Intern
Here is the result of my test

1.The Intern compared to the local variable did test true.
2. I created a new object and returned a value from a function and
compared it to the intern. It tested true.
3. I created a second intern and tried to compare it to the ToString
Method of the class created in #2. It failed.
4. I tried casting both strings to objects, it also failed but I am
not sure if this is expected, the MSDN sample show it done this way.
Click
here to see the MSDN Sample.
5. I tried to compare the FullName Property of a type to the intern,
it failed.
6. Just for the heck of it, I tried converting the string to a byte
array and back to a string to see if it would then compare, it
failed.
7. I created a local variable with the same name as #6 and compared
it to the intern, it passed.
I am totally confused at to the expected behavior of the comparing
an intern string to a String variable with the following syntax:
internVariable Is localvariable
Why would a Return value of String from a Function compare but a
String returned from the ToStringMethod or the FullName Property of
the Type class fail?
Here is the first few lines of source code.
'Here is the intern string
Private
internString
As
String =
String.Intern("This
is a Test")
Private
internObjectString
As
String =
String.Intern("VBTestConsoleApp.InternTest")
Sub
Main()
Dim
regString
As
String =
"This is a Test"
Dim
o
As
New
InternTest()
Dim
objectGetString
As
String =
o.GetString()
Dim
objectToString
As
String =
o.ToString()
Console.WriteLine("String.Intern
VB.NET Test")
Console.WriteLine(String.Concat("Is
regString equal to the internString: ", (internString
Is
regString),
Environment.NewLine))
'This Passes
Console.WriteLine("Now
we see if the return from a function GetString.")
Console.WriteLine(String.Concat("Is
objectGetString equal to the internString: ", (internString
Is
objectGetString),
Environment.NewLine))
'This Passes
Console.WriteLine("Now
we see if the return from a ToString of an object.")
Console.WriteLine(String.Concat("Is
objectToString equal to the internString: ", (internObjectString
Is
objectToString)))
'This Fails
Please download the entire VS.NET sample
here.
If you know the problem, could you please email me at
nntp@fesersoft.com
Thank you.
Joe Feser
|