469,312 Members | 2,440 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,312 developers. It's quick & easy.

String Array to Int Array

AMP
Hello
I get 2 errors in the following code that I cant Debug without some
help.
They are both in the TryParse function. Seems simple enough.The Errors
are Below.
private void ConvertLabelStringToArray(string p)
{
string[] CardsToBeSortedArray = p.Split(' ');
uint[] IntCard = new uint[CardsToBeSortedArray.Length];
foreach (string Card in CardsToBeSortedArray)
{
UInt32.TryParse(Card, out IntCard);

}

}

Error 1 The best overloaded method match for 'uint.TryParse(string, out
uint)' has some invalid arguments
Error 2 Argument '2': cannot convert from 'out uint[]' to 'out uint'

I would appreciate some enlightening
Thanks
Mike

Nov 28 '06 #1
17 13523
Hello
I get 2 errors in the following code that I cant Debug without some
help.
They are both in the TryParse function. Seems simple enough.The Errors
are Below.
private void ConvertLabelStringToArray(string p)
{
string[] CardsToBeSortedArray = p.Split(' ');
uint[] IntCard = new uint[CardsToBeSortedArray.Length];
foreach (string Card in CardsToBeSortedArray)
{
UInt32.TryParse(Card, out IntCard);
}

}

Error 1 The best overloaded method match for 'uint.TryParse(string,
out
uint)' has some invalid arguments
Error 2 Argument '2': cannot convert from 'out uint[]' to 'out uint'
I would appreciate some enlightening
You can't pass a uint[] where only a uint is expected. You have to do this:

private void ConvertLabelStringToArray(string p)
{
string[] CardsToBeSortedArray = p.Split(' ');
uint[] IntCard = new uint[CardsToBeSortedArray.Length];
for (int i = 0; i < CardsToBeSortedArray.Length; i++)
UInt32.TryParse(CardsToBeSortedArray[i], out IntCard[i]);
}

But, since you're using .NET Framework 2.0, you might try using the Array.ConvertAll<TInput,
TOutputmethod instead:

private static void ConvertLabelStringToArray(string p)
{
string[] CardsToBeSortedArray = p.Split(' ');
uint[] IntCard = Array.ConvertAll<string, uint>(CardsToBeSortedArray, delegate(string
card)
{
uint result;
UInt32.TryParse(card, out result);
return result;
});
}
Best Regards,
Dustin Campbell
Developer Express Inc.
Nov 28 '06 #2
AMP wrote:
Hello
I get 2 errors in the following code that I cant Debug without some
help.
They are both in the TryParse function. Seems simple enough.The Errors
are Below.
private void ConvertLabelStringToArray(string p)
{
string[] CardsToBeSortedArray = p.Split(' ');
uint[] IntCard = new uint[CardsToBeSortedArray.Length];
foreach (string Card in CardsToBeSortedArray)
{
UInt32.TryParse(Card, out IntCard);

}

}

Error 1 The best overloaded method match for 'uint.TryParse(string, out
uint)' has some invalid arguments
Error 2 Argument '2': cannot convert from 'out uint[]' to 'out uint'
As the second error states, TryParse needs a 'out uint' as its second
parameter, you are passing 'out uint[]'. You need to pass a specific index
of the array. I would rework the loop as follows:

private void ConvertLabelStringToArray(string p)
{
string[] CardsToBeSortedArray = p.Split(' ');
uint[] IntCard = new uint[CardsToBeSortedArray.Length];
for (int i = 0; i < CardsToBeSortedArray.Length; i++)
{
uint.TryParse(CardsToBeSortedArray[i], out IntCard[i]);
}
}
--
Tom Porterfield

Nov 28 '06 #3
Dustin Campbell wrote:
You can't pass a uint[] where only a uint is expected. You have to do
this:
private void ConvertLabelStringToArray(string p)
{
string[] CardsToBeSortedArray = p.Split(' ');
uint[] IntCard = new uint[CardsToBeSortedArray.Length];
for (int i = 0; i < CardsToBeSortedArray.Length; i++)
UInt32.TryParse(CardsToBeSortedArray[i], out IntCard[i]);
}

But, since you're using .NET Framework 2.0, you might try using the
Array.ConvertAll<TInput, TOutputmethod instead:

private static void ConvertLabelStringToArray(string p)
{
string[] CardsToBeSortedArray = p.Split(' ');
uint[] IntCard = Array.ConvertAll<string, uint>(CardsToBeSortedArray,
delegate(string card)
{
uint result;
UInt32.TryParse(card, out result);
return result;
});
}
Help me understand something. The second implementation is more code,
requires generics and delegates, and is harder to read, than the first. So
what are the advantages of doing it that way?
--
Tom Porterfield

Nov 28 '06 #4
Dustin Campbell wrote:
>
>You can't pass a uint[] where only a uint is expected. You have to do
this:
private void ConvertLabelStringToArray(string p)
{
string[] CardsToBeSortedArray = p.Split(' ');
uint[] IntCard = new uint[CardsToBeSortedArray.Length];
for (int i = 0; i < CardsToBeSortedArray.Length; i++)
UInt32.TryParse(CardsToBeSortedArray[i], out IntCard[i]);
}
But, since you're using .NET Framework 2.0, you might try using the
Array.ConvertAll<TInput, TOutputmethod instead:

private static void ConvertLabelStringToArray(string p)
{
string[] CardsToBeSortedArray = p.Split(' ');
uint[] IntCard = Array.ConvertAll<string, uint>(CardsToBeSortedArray,
delegate(string card)
{
uint result;
UInt32.TryParse(card, out result);
return result;
});
}
Help me understand something. The second implementation is more code,
requires generics and delegates, and is harder to read, than the
first. So what are the advantages of doing it that way?
You gave several reasons against the second version. I'll address each:

1. The second implementation is more code.

Yes, the second version contains 14 more characters and two more lines. However,
it removes a loop that is arguably difficult to read. I used an anonymous
method to keep the code in one method so that comparisans could be made.
In reality, I might write it like this:

private uint CardToUInt32(string card)
{
uint result;
UInt32.TryParse(card, out result);
return result;
}
private void ConvertLabelStringToArray(string p)
{
string[] CardsToBeSortedArray = p.Split(' ');
uint[] IntCard = Array.ConvertAll<string, uint>(CardsToBeSortedArray, CardToUInt32);
}

That pushes the anonymous method into its own method and promotes reuse and
increases readability.

Note that there are very strong reasons to use anonymous methods in some
cases because they actually reduse code.

2. Requires generics and delegates.

I fail to see how using language features in C# 2.0 is a disadvantage as
it is clear that the code in the original post is using .NET Framework 2.0.
You can tell because UInt32.TryParse() is being used. That didn't exist before
2.0.

3. It's harder to read.

True. As best practice, code that uses an anonymous method for good reason
(e.g. to create a closure) should be moved to its own method. But, if you
look at the code that I posted for point #1, it is easy to create a situation
where the code is actually much better than falling back on a for-loop. It's
true that the for-loop is marginally faster but the difference is quite small.

Best Regards,
Dustin Campbell
Developer Express Inc.
Nov 28 '06 #5
"Dustin Campbell" <du*****@no-spam-pleasedevexpress.comschrieb im
Newsbeitrag news:c1**************************@news.microsoft.c om...
<snip>
private static void ConvertLabelStringToArray(string p)
{
string[] CardsToBeSortedArray = p.Split(' ');
uint[] IntCard = Array.ConvertAll<string, uint>(CardsToBeSortedArray,
delegate(string card) {
uint result;
UInt32.TryParse(card, out result);
return result;
});
}
Could also be written as:

private static void ConvertLabelStringToArray(string p)
{
string[] CardsToBeSortedArray = p.Split(' ');
uint[] IntCard = Array.ConvertAll<string, uint>(CardsToBeSortedArray,
delegate(string
card)
{ retun UInt32.TryParse(card, out result);});
}

Nov 29 '06 #6
"Tom Porterfield" <tp******@mvps.orgschrieb im Newsbeitrag
news:OY**************@TK2MSFTNGP06.phx.gbl...
Dustin Campbell wrote:
>You can't pass a uint[] where only a uint is expected. You have to do
this:
private void ConvertLabelStringToArray(string p)
{
string[] CardsToBeSortedArray = p.Split(' ');
uint[] IntCard = new uint[CardsToBeSortedArray.Length];
for (int i = 0; i < CardsToBeSortedArray.Length; i++)
UInt32.TryParse(CardsToBeSortedArray[i], out IntCard[i]);
}

But, since you're using .NET Framework 2.0, you might try using the
Array.ConvertAll<TInput, TOutputmethod instead:

private static void ConvertLabelStringToArray(string p)
{
string[] CardsToBeSortedArray = p.Split(' ');
uint[] IntCard = Array.ConvertAll<string, uint>(CardsToBeSortedArray,
delegate(string card)
{
uint result;
UInt32.TryParse(card, out result);
return result;
});
}

Help me understand something. The second implementation is more code,
requires generics and delegates, and is harder to read, than the first.
So what are the advantages of doing it that way?
If you find it harder to read, simply don't use it. ;-)
Nov 29 '06 #7
No it couldn't; uint.TryParse returns a bool, where-as Converter<string,
uintmust accept a string and return a uint. It is not compatible.

Marc
Nov 29 '06 #8
On Wed, 29 Nov 2006 11:37:36 +0100, Christof Nordiek wrote:
If you find it harder to read, simply don't use it. ;-)
That's one of the poorest excuses I've heard to not use something.
--
Tom Porterfield
Nov 29 '06 #9
I'm not so convinced in this case...

Look at the pros and cons of doing it this way...

pros: none; previous example showed it was very simple to do in another
(very clear) way
cons: harder to read *PLUS* all the other (very valid) comments [although
generics at least should be ubiquitious by now]

So if it doesn't gain you anything, but makes the code more complex and
hence probably both buggier (see TryParse anti-example) and harder to
debug... doesn't that just shout *do it the easy way!*

Granted there are definite cases for using delegates and methods such as
ConvertAll<>, but I'm not sure that this is one of them, and as such I would
counter "poorest excuse" with "most sensible design decision".

Marc
Nov 29 '06 #10
On Wed, 29 Nov 2006 11:34:42 +0100, Christof Nordiek wrote:
Could also be written as:

private static void ConvertLabelStringToArray(string p)
{
string[] CardsToBeSortedArray = p.Split(' ');
uint[] IntCard = Array.ConvertAll<string, uint>(CardsToBeSortedArray,
delegate(string
card)
{ retun UInt32.TryParse(card, out result);});
}
No, that doesn't compile, and even if fixed doesn't work the same way.
You've never declared result. Even if you did, you are returning the
return value of TryParse which is a bool that indicates success or failure
of the ability to parse the string into a uint. What he wants to place in
the array is the value of the uint after parsing, meaning he needs back the
undeclared result as Dustin has in his implementation.
--
Tom Porterfield
Nov 29 '06 #11
Marc and Tom are right, this simply isn't right. My fault.

"Christof Nordiek" <cn@nospam.deschrieb im Newsbeitrag
news:Ow**************@TK2MSFTNGP02.phx.gbl...
"Dustin Campbell" <du*****@no-spam-pleasedevexpress.comschrieb im
Newsbeitrag news:c1**************************@news.microsoft.c om...
<snip>
>private static void ConvertLabelStringToArray(string p)
{
string[] CardsToBeSortedArray = p.Split(' ');
uint[] IntCard = Array.ConvertAll<string, uint>(CardsToBeSortedArray,
delegate(string card) {
uint result;
UInt32.TryParse(card, out result);
return result;
});
}

Could also be written as:

private static void ConvertLabelStringToArray(string p)
{
string[] CardsToBeSortedArray = p.Split(' ');
uint[] IntCard = Array.ConvertAll<string, uint>(CardsToBeSortedArray,
delegate(string
card)
{ retun UInt32.TryParse(card, out result);});
}

Nov 29 '06 #12
>If you find it harder to read, simply don't use it. ;-)
>>
That's one of the poorest excuses I've heard to not use something.
Agreed. My goal was simply to show that the Array.ConvertAll<TInput, TOutput>
method exists and can be used for simple array conversion operations. The
truth is, I think that the original method is difficult to read and doesn't
really exhibit good design. This helped the anonymous method to overcomplicate
things even further because anonymous methods *should* be short. For reference,
here it is again:

private void ConvertLabelStringToArray(string p)
{
string[] CardsToBeSortedArray = p.Split(' ');
uint[] IntCard = new uint[CardsToBeSortedArray.Length];
foreach (string Card in CardsToBeSortedArray)
{
UInt32.TryParse(Card, out IntCard);
}
}

And a working, sort-of-complete version:

private uint[] ConvertLabelStringToArray(string p)
{
string[] CardsToBeSortedArray = p.Split(' ');
uint[] IntCard = new uint[CardsToBeSortedArray.Length];
for (int i = 0; i < CardsToBeSortedArray.Length; i++)
UInt32.TryParse(CardsToBeSortedArray[i], out IntCard[i]);

return IntCard;
}

IMO, using UInt32.TryParse this way makes the method far more difficult to
read. It's almost as if UInt32.TryParse is being shoe-horned in. Because
of the way in which UInt32.TryParse is designed, a mental jump has to be
made because the assignment actually happens at the end of the method call.
Also, you must to have the intimate knowledge that UInt32.TryParse will set
IntCard[i] to zero if it fails. So, the code in this method is semantically
coupled to UInt32.TryParse and that makes a very simple operation much harder
to read.

Here's one way to improve readability:

private uint ParseCard(string card)
{
uint result;
if (!UInt32.TryParse(card, out result))
result = 0;

return result;
}
private uint[] ConvertLabelStringToArray(string p)
{
string[] CardsToBeSortedArray = p.Split(' ');
uint[] IntCard = new uint[CardsToBeSortedArray.Length];
for (int i = 0; i < CardsToBeSortedArray.Length; i++)
IntCard[i] = ParseCard(CardsToBeSortedArray[i]);

return IntCard;
}

The call to UInt32.TryParse has been extracted to a more readable method
called "ParseCard". In addition, the semantic coupling to UInt32.TryParse
has been removed because the code no longer assumes that zero is returned
on failure. So, if Microsoft does anything insane or if the code runs on
a different .NET platform, we're protected against changes there.

And now, Array.ConvertAll can be used to reduce the code:

private uint[] ConvertLabelStringToArray(string p)
{
string[] CardsToBeSortedArray = p.Split(' ');
return Array.ConvertAll<string, uint>(CardsToBeSortedArray, delegate(string
card) { return ParseCard(card); });
}

Or, if you prefer not using an anonymous method, you can can simple specify
the method and reduce the code further:

private uint[] ConvertLabelStringToArray(string p)
{
string[] CardsToBeSortedArray = p.Split(' ');
return Array.ConvertAll<string, uint>(CardsToBeSortedArray, ParseCard);
}

I actually dislike that code a bit because it isn't clear what is going on.
It's harder to see that ParseCard is a method that is going to be called
with a specific parameter and its return value used. So, I prefer the clarity
of an anonymous method -- even though I detest the "delegate" keyword it
requires. That said, lambda expressions realy excite me because I can improve
the readability of the Array.ConvertAll call in some cool ways. In C# 3.0,
if a lambda expression is used, the compiler has enough information to figure
out what the generic arguments of Array.ConvertAll should be. So, the code
becomes this:

private uint[] ConvertLabelStringToArray(string p)
{
string[] CardsToBeSortedArray = p.Split(' ');
return Array.ConvertAll(CardsToBeSortedArray, card =ParseCard(card));
}

I like this most of all because it gives me the readable qualities that I
liked about the anonymous method example and makes it more succinct at the
same time. But, then again, I've been playing with lambda expressions a great
deal so I'm pretty comfortable with them.

Best Regards,
Dustin Campbell
Developer Express Inc.
Nov 29 '06 #13
"Tom Porterfield" <tp******@mvps.orgwrote:
>Help me understand something. The second implementation is more code,
requires generics and delegates, and is harder to read, than the first. So
what are the advantages of doing it that way?
You must be new to IT, yes?

:)
Nov 29 '06 #14
Lucian Wischik wrote:
"Tom Porterfield" <tp******@mvps.orgwrote:
>Help me understand something. The second implementation is more code,
requires generics and delegates, and is harder to read, than the first.
So what are the advantages of doing it that way?

You must be new to IT, yes?
I guess. I've only been in IT as a programmer, now tech lead and
consultant, for nine years. You?
--
Tom Porterfield

Nov 29 '06 #15
>Help me understand something. The second implementation is more
>code, requires generics and delegates, and is harder to read, than
the first. So what are the advantages of doing it that way?
You must be new to IT, yes?
Please don't make insulting comments.

Best Regards,
Dustin Campbell
Developer Express Inc.
Nov 29 '06 #16
Lucian Wischik <lu***@wischik.comwrote:
>"Tom Porterfield" <tp******@mvps.orgwrote:
>>Help me understand something. The second implementation is more code,
requires generics and delegates, and is harder to read, than the first. So
what are the advantages of doing it that way?

You must be new to IT, yes?
:)
Clarification: as indicated by the smiley, it was a joke. The joke is
that in software engineering we like overly-complicated solutions with
too many layers of abstractions. It's the same joke as this one:

------------

How the way people code "Hello World" varies depending on their age
and job:

High School/Jr.High
10 PRINT "HELLO WORLD"
20 END
First year in College
program Hello(input, output)
begin
writeln('Hello World');
end.
Senior year in College
(defun hello
(print
(cons 'Hello (list 'World))))
New professional
#include <stdio.h>

void main(void)
{
char *message[] = {"Hello ", "World"};
int i;
for(i = 0; i < 2; ++i)
printf("%s", message[i]);
printf("\n");
}
Seasoned professional
#include <iostream.h>
#include <string.h>
class string
{
private:
int size;
char *ptr;
public:
string() : size(0), ptr(new char('\0')) {}
string(const string &s) : size(s.size)
{
ptr = new char[size + 1];
strcpy(ptr, s.ptr);
}
~string()
{
delete [] ptr;
}
friend ostream &operator <<(ostream &, const string &);
string &operator=(const char *);
};

ostream &operator<<(ostream &stream, const string &s)
{
return(stream << s.ptr);
}
string &string::operator=(const char *chrs)
{
if (this != &chrs)
{
delete [] ptr;
size = strlen(chrs);
ptr = new char[size + 1];
strcpy(ptr, chrs);
}
return(*this);
}
int main()
{
string str;
str = "Hello World";
cout << str << endl;
return(0);
}
Nov 29 '06 #17
On Wed, 29 Nov 2006 12:14:35 -0800, Lucian Wischik <lu***@wischik.comwrote:

[snip]

Good Lucian! I understood your original tongue in cheek joke.

I've mastered every one of the variations below. ;O)
>------------

How the way people code "Hello World" varies depending on their age
and job:

High School/Jr.High
10 PRINT "HELLO WORLD"
20 END
First year in College
program Hello(input, output)
begin
writeln('Hello World');
end.
Senior year in College
(defun hello
(print
(cons 'Hello (list 'World))))
New professional
#include <stdio.h>

void main(void)
{
char *message[] = {"Hello ", "World"};
int i;
for(i = 0; i < 2; ++i)
printf("%s", message[i]);
printf("\n");
}
Seasoned professional
#include <iostream.h>
#include <string.h>
class string
{
private:
int size;
char *ptr;
public:
string() : size(0), ptr(new char('\0')) {}
string(const string &s) : size(s.size)
{
ptr = new char[size + 1];
strcpy(ptr, s.ptr);
}
~string()
{
delete [] ptr;
}
friend ostream &operator <<(ostream &, const string &);
string &operator=(const char *);
};

ostream &operator<<(ostream &stream, const string &s)
{
return(stream << s.ptr);
}
string &string::operator=(const char *chrs)
{
if (this != &chrs)
{
delete [] ptr;
size = strlen(chrs);
ptr = new char[size + 1];
strcpy(ptr, chrs);
}
return(*this);
}
int main()
{
string str;
str = "Hello World";
cout << str << endl;
return(0);
}
Good luck with your project,

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
Nov 30 '06 #18

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

16 posts views Thread by Don Starr | last post: by
7 posts views Thread by al | last post: by
4 posts views Thread by songkv | last post: by
22 posts views Thread by spike | last post: by
4 posts views Thread by Simon Schaap | last post: by
8 posts views Thread by Jeff Johnson | last post: by
17 posts views Thread by Chad Myers | last post: by
11 posts views Thread by Zordiac | last post: by
14 posts views Thread by Shhnwz.a | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.