string array differences

  • Thread starter =?ISO-8859-1?Q?G=F6ran_Andersson?=
  • Start date
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Morten71 said:
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.
 
B

Branco Medeiros

Morten71 wrote:
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.

I guess you meant "1 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.

Are you sure there isn't an extra space before "InvoiceDate"?

HTH.

Regards,

Branco.
 
M

Morten71

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
 

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

Top