473,659 Members | 2,656 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 ConvertLabelStr ingToArray(stri ng p)
{
string[] CardsToBeSorted Array = p.Split(' ');
uint[] IntCard = new uint[CardsToBeSorted Array.Length];
foreach (string Card in CardsToBeSorted Array)
{
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 13727
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 ConvertLabelStr ingToArray(stri ng p)
{
string[] CardsToBeSorted Array = p.Split(' ');
uint[] IntCard = new uint[CardsToBeSorted Array.Length];
foreach (string Card in CardsToBeSorted Array)
{
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 ConvertLabelStr ingToArray(stri ng p)
{
string[] CardsToBeSorted Array = p.Split(' ');
uint[] IntCard = new uint[CardsToBeSorted Array.Length];
for (int i = 0; i < CardsToBeSorted Array.Length; i++)
UInt32.TryParse (CardsToBeSorte dArray[i], out IntCard[i]);
}

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

private static void ConvertLabelStr ingToArray(stri ng p)
{
string[] CardsToBeSorted Array = p.Split(' ');
uint[] IntCard = Array.ConvertAl l<string, uint>(CardsToBe SortedArray, 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 ConvertLabelStr ingToArray(stri ng p)
{
string[] CardsToBeSorted Array = p.Split(' ');
uint[] IntCard = new uint[CardsToBeSorted Array.Length];
foreach (string Card in CardsToBeSorted Array)
{
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 ConvertLabelStr ingToArray(stri ng p)
{
string[] CardsToBeSorted Array = p.Split(' ');
uint[] IntCard = new uint[CardsToBeSorted Array.Length];
for (int i = 0; i < CardsToBeSorted Array.Length; i++)
{
uint.TryParse(C ardsToBeSortedA rray[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 ConvertLabelStr ingToArray(stri ng p)
{
string[] CardsToBeSorted Array = p.Split(' ');
uint[] IntCard = new uint[CardsToBeSorted Array.Length];
for (int i = 0; i < CardsToBeSorted Array.Length; i++)
UInt32.TryParse (CardsToBeSorte dArray[i], out IntCard[i]);
}

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

private static void ConvertLabelStr ingToArray(stri ng p)
{
string[] CardsToBeSorted Array = p.Split(' ');
uint[] IntCard = Array.ConvertAl l<string, uint>(CardsToBe SortedArray,
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 ConvertLabelStr ingToArray(stri ng p)
{
string[] CardsToBeSorted Array = p.Split(' ');
uint[] IntCard = new uint[CardsToBeSorted Array.Length];
for (int i = 0; i < CardsToBeSorted Array.Length; i++)
UInt32.TryPars e(CardsToBeSort edArray[i], out IntCard[i]);
}
But, since you're using .NET Framework 2.0, you might try using the
Array.ConvertA ll<TInput, TOutputmethod instead:

private static void ConvertLabelStr ingToArray(stri ng p)
{
string[] CardsToBeSorted Array = p.Split(' ');
uint[] IntCard = Array.ConvertAl l<string, uint>(CardsToBe SortedArray,
delegate(strin g card)
{
uint result;
UInt32.TryPars e(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(st ring card)
{
uint result;
UInt32.TryParse (card, out result);
return result;
}
private void ConvertLabelStr ingToArray(stri ng p)
{
string[] CardsToBeSorted Array = p.Split(' ');
uint[] IntCard = Array.ConvertAl l<string, uint>(CardsToBe SortedArray, 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-pleasedevexpres s.comschrieb im
Newsbeitrag news:c1******** *************** ***@news.micros oft.com...
<snip>
private static void ConvertLabelStr ingToArray(stri ng p)
{
string[] CardsToBeSorted Array = p.Split(' ');
uint[] IntCard = Array.ConvertAl l<string, uint>(CardsToBe SortedArray,
delegate(string card) {
uint result;
UInt32.TryParse (card, out result);
return result;
});
}
Could also be written as:

private static void ConvertLabelStr ingToArray(stri ng p)
{
string[] CardsToBeSorted Array = p.Split(' ');
uint[] IntCard = Array.ConvertAl l<string, uint>(CardsToBe SortedArray,
delegate(string
card)
{ retun UInt32.TryParse (card, out result);});
}

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

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

private static void ConvertLabelStr ingToArray(stri ng p)
{
string[] CardsToBeSorted Array = p.Split(' ');
uint[] IntCard = Array.ConvertAl l<string, uint>(CardsToBe SortedArray,
delegate(strin g 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<strin g,
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

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

Similar topics

16
17409
by: Don Starr | last post by:
When applied to a string literal, is the sizeof operator supposed to return the size of the string (including nul), or the size of a pointer? For example, assuming a char is 1 byte and a char * is 4 bytes, should the following yield 4, 5, of something else? (And, if something else, what determines the result?) char x = "abcd"; printf( "%d\n", sizeof( x ) ); -Don
7
4352
by: al | last post by:
char s = "This string literal"; or char *s= "This string literal"; Both define a string literal. Both suppose to be read-only and not to be modified according to Standard. And both have type of "const char *". Right? But why does the compiler I am using allow s to be modified, instead of generating compile error?
4
5387
by: songkv | last post by:
Hi, I am trying to reassign an array of char to a string literal by calling a function. In the function I use pointer-to-pointer since I want to reassign the "string array pointer" to the string literal. But the second printf seems to give me garbage. Any advise on what I am doing wrong? Thanx
22
17169
by: spike | last post by:
How do i reset a string? I just want to empty it som that it does not contain any characters Say it contains "hello world" at the time... I want it to contain "". Nothing that is.. Thanx
4
8807
by: Simon Schaap | last post by:
Hello, I have encountered a strange problem and I hope you can help me to understand it. What I want to do is to pass an array of chars to a function that will split it up (on every location where a * occurs in the string). This split function should allocate a 2D array of chars and put the split results in different rows. The listing below shows how I started to work on this. To keep the program simple and help focus the program the...
14
15023
by: Bob | last post by:
I have a function that takes in a list of IDs (hundreds) as input parameter and needs to pass the data to another step as a comma delimited string. The source can easily create this list of IDs in a comma-delimited string or string array. I don't want it to be a string because I want to overload this function, and it's sister already uses a string input parameter. Now if I define the function to take in a string array, it solves my...
8
13943
by: Jeff Johnson | last post by:
Hi, I've begun converting an ASP site over to .NET and I'm a novice at both the new platform as well as C#. I have a COM+ object that returns a string array when it is called. The size of the array can vary depending on the parameters passed. What I need to do is loop through the returned array and if applicable write the array element to the screen.
17
4653
by: Chad Myers | last post by:
I've been perf testing an application of mine and I've noticed that there are a lot (and I mean A LOT -- megabytes and megabytes of 'em) System.String instances being created. I've done some analysis and I'm led to believe (but can't yet quantitatively establish as fact) that the two basic culprits are a lot of calls to: 1.) if( someString.ToLower() == "somestring" ) and
11
17638
by: Zordiac | last post by:
How do I dynamically populate a string array? I hope there is something obvious that I'm missing here Option Strict On dim s() as string dim sTmp as string = "test" dim i as integer s(i)=new string(test) Above line gives - error implicit conversion string to 1-dim array of
14
4068
by: Shhnwz.a | last post by:
Hi, I am in confusion regarding jargons. When it is technically correct to say.. String or Character Array.in c. just give me your perspectives in this issue. Thanx in Advance.
0
8427
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8851
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8746
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8525
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8627
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7356
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6179
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.