find closest item in keyed collection

  • Thread starter Thread starter Steve Richter
  • Start date Start date
How about
string[] X = new string[]{"Anne", "Bob", "Chris", "Donna", "Elizabeth",
"Glenn", "Franklin", "Helene"};
The original question presented was how to get a "soft" match in an array
using BinarySearch, i.e.: if no match, return the next highest value in
the array; so far as I know, there's no (straightforward) way to get
there from string[] x = {..,..,.. } syntax ...

I did Add "off topic" right before I asked (which you edited out).
I have no quibble over the Binary search suggestion, It is what I would
have suggested.
My question has nothing to do with that.
I simply wanted to know why you would Array.CreateInstance instead of the
more common syntax for array creation?
Is there some benefit that I am unaware of?

Bill,

First of all, your comment was not off-topic ....
I simply wanted to know why you would Array.CreateInstance instead of the
more common syntax for array creation?

that's what I responded to .... you can't do this:

string[] X = new string[]{"Anne", "Bob", "Chris", "Donna",
"Elizabeth", "Glenn", "Franklin", "Helene"};

X.BinarySearch("DonnaZ");

Do you know a way to initiate a BinarySearch using the array initialization
syntax you propose here?
 
Bill,

First of all, your comment was not off-topic ....
I simply wanted to know why you would Array.CreateInstance instead of
the more common syntax for array creation?

that's what I responded to .... you can't do this:

string[] X = new string[]{"Anne", "Bob", "Chris", "Donna",
"Elizabeth", "Glenn", "Franklin", "Helene"};

X.BinarySearch("DonnaZ");

Do you know a way to initiate a BinarySearch using the array
initialization syntax you propose here?

Array.BinarySearch(X,"DonnaZ");
 
[...]
I simply wanted to know why you would Array.CreateInstance instead of
the
more common syntax for array creation?

that's what I responded to .... you can't do this:

string[] X = new string[]{"Anne", "Bob", "Chris", "Donna",
"Elizabeth", "Glenn", "Franklin", "Helene"};

X.BinarySearch("DonnaZ");

You can't do that even initializing the array as you did. There's no
Array.BinarySearch() overload that takes a single parameter. There's not
even an instance method version of BinarySearch.
Do you know a way to initiate a BinarySearch using the array
initialization
syntax you propose here?

There are a number of overloads for Array.BinarySearch. Any of them would
work equally well, whether you initialize the array using the syntax you
posted or the syntax Bill posted. For example:

Array.BinarySearch(X, "DonnaZ");

I'm with Bill. I don't see any advantage at all to using the syntax you
posted. It's incredibly unwieldy, difficult to read, and doesn't even
provide a typed array. I'm at a loss to understand why you'd prefer that
syntax to the normal array declaration syntax.

Pete
 
I simply wanted to know why you would Array.CreateInstance instead of
the more common syntax for array creation?

that's what I responded to .... you can't do this:

string[] X = new string[]{"Anne", "Bob", "Chris", "Donna",
"Elizabeth", "Glenn", "Franklin", "Helene"};

X.BinarySearch("DonnaZ");

Do you know a way to initiate a BinarySearch using the array
initialization syntax you propose here?
Array.BinarySearch(X,"DonnaZ");


There ya go ... I learn something new every day :)

I do think the string[] X = { ... } is more useful for relatively small
lists like this example, though ... (where you really can just walk the
array instead of using a BinarySearch)
 
<< I'm with Bill. I don't see any advantage at all to using the syntax you
posted. It's incredibly unwieldy, difficult to read, and doesn't even
provide a typed array >>

yeah, it's awkward but more because of the series of arr.SetValue(string,
n); statements than anything else; I *don't* prefer it; my focus from the
original question was the behavior of BinarySearch to fetch the next highest
value on no-match ... not the mechanics of constructing the array to search


<< I'm at a loss to understand why you'd prefer that syntax to the normal
array declaration syntax >>

what do you consider "normal" ? string[] x = { a, b, c } is fine, but only
for a smallish sample of known elements ...

anyway, do you really think there's an incredible difference in readability
or wieldiness in these two:

string[] x = new string[4500];
for (int n = 0; n < 4500; n++)
{
x[n] = "Value" + n.ToString();
}


Array x = Array.CreateInstance( typeof(string), 4500 );
for (int n = 0; n < 4500; n++)
{
x.SetValue( "Value" + n.ToString(), n );
}

I think I'd give the first one a slight edge mainly because it has a more
familiar feel to "C" family programmers .... but that's about it ... OTOH,
it would be what I would normally use based on that familiarity factor ...
 
yeah, it's awkward but more because of the series of arr.SetValue(string,
n); statements than anything else; I *don't* prefer it; my focus from
the
original question was the behavior of BinarySearch to fetch the next
highest
value on no-match ... not the mechanics of constructing the array to
search

If you don't prefer that syntax, then why did you write it? Was it that
you believed that using that syntax was the only way to get access to the
BinarySearch() method? Or was there something else that compelled you to
write it that way?

I don't prefer that syntax, and I would _never_ initialize an array of a
known type using that syntax.
<< I'm at a loss to understand why you'd prefer that syntax to the normal
array declaration syntax >>

what do you consider "normal" ? string[] x = { a, b, c } is fine, but
only
for a smallish sample of known elements ...

I don't know what that means. No matter how many elements you have, it's
always easier to write a list of them, than to write a list of them
contained in a long list of calls to Array.SetValue(). In the former
case, you have each item, plus a comma and optionally a space. In the
latter case, you have each item, plus a semi-colon, and optionally a space
and/or newline, to which you also have to add the name of the array plusa
period plus the method name "SetValue" plus a pair of parentheses.

Can you describe a scenario in which the syntax you've offered is more
compact or is in any describable way better than the normal syntax?

And yes...the "string[] x = ..." syntax is definitely the normal syntax.
You're the first person I've ever seen use the other syntax when the type
of the array elements was known.
anyway, do you really think there's an incredible difference in
readability
or wieldiness in these two:

Yes, absolutely. I find the latter arbitrarily and significantly awkward.

Besides, with the first example you get a variable that has the actual
type of the array. With the latter, there's no compile-time protection
against coding errors, nor do you get any help with Intellisense for that
matter.

I see literally no value at all in the second example, except of course
when you don't know the array type in the first place (but then you
couldn't write "typeof(string)"...you'd have to have an actual Type
variable to pass in there).

Pete
 

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

Back
Top