473,325 Members | 2,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

string array differences


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
Feb 14 '07 #1
3 1578
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
Feb 15 '07 #2
Morten71 wrote:
<snip>
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.

Feb 15 '07 #3
Morten,

In my idea is there written that the method search for objects in an array
of objects.

Nowhere is written that it has any relation with the string.indexof which
search for characterstrings inside a characterstring.

http://msdn2.microsoft.com/en-us/lib...y.indexof.aspx

Cor

<Morten71schreef in bericht
news:fb********************************@4ax.com...
>
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

Feb 15 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

37
by: Kevin C | last post by:
Quick Question: StringBuilder is obviously more efficient dealing with string concatenations than the old '+=' method... however, in dealing with relatively large string concatenations (ie,...
28
by: Davy | last post by:
Hi all, I found char x={"my"}; can be compiled. But char x; x={"my"}; can not be compiled.
6
by: Dan V. | last post by:
I would like to create a 2D string list (2D ArrayList ???). I would like to pass in a table or query as a parameter and have both columns transform into a 2D ArrayList. When I sort the one...
3
by: Asha | last post by:
greetings, i have some questions below, what are the differences between private string _strVal = string.Empty; and _strVal = null; does the string.Empty; allocate memory for it? how about...
1
by: Samuel R. Neff | last post by:
Are there any differences between using Array.Length and Array.GetUpperBound(0) on a one-dimensional array? We have a team of developers and most people use Array.Length but one developer uses...
18
by: Kyro | last post by:
New to C# (migrating from Delphi) and I'm not sure how to get a delimited string into an array of string. input: string looks like - 01-85-78-15-Q11 output: an array with elements - ...
6
by: Andrea | last post by:
Hi, suppose that I have a string that is an hexadecimal number, in order to print this string I have to do: void print_hex(unsigned char *bs, unsigned int n){ int i; for (i=0;i<n;i++){...
5
by: polas | last post by:
Good morning, I have a quick question to clear up some confusion in my mind. I understand that using a string literal in a declaration such as char *p = "string literal" declares a pointer to...
12
by: George2 | last post by:
Hello everyone, Suppose we defined a string buffer (array), like this, char array = "hello world"; char buf Sometimes, I noticed that we either use,
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.