Morten71 wrote:
I have a strange problem.
I have a local string() var I populate this way:
clmns() As String = {"InvoiceNo", "InvoiceDate"}
When I call:
Array.IndexOf(clmns,"InvoiceDate") I get 0 (zero) as expected.
If I fetch the values from the web.config this way:
clmns() As String =
ConfigurationSettings.AppSettings("clmns").Split(" ,")
(in my web.config I have: <add key="clmns" value="InvoiceNo,
InvoiceDate" />)
I get -1 when I call:
Array.IndexOf(clmns,"InvoiceDate")
Looking at the clmns var (regardless of how it is populated) in my
debug window reveals identical structure and type.
Do the split method produce a different type than the = {"", ""} way?
Morten
The problem is that you are comparing the strings by reference, not by
value.
Literal strings are stored as constants in the code. The compiler looks
for identical strings in the code where it can reuse the contants. When
you create the array from literal strings and compare to another literal
string, the matching strings aren't just equal, they are actually the
same string.
When you create the array by splitting a string, it produces new strings
at runtime, so the compiler can't reuse the constants in the code. The
matching strings have the same value, but they are two separate strings.
As you are comparing the strings by reference and not by value, they
won't match.
To match the strings by value, you should use the generic form of the
Array.IndexOf method.
--
Göran Andersson
_____
http://www.guffa.com