Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old January 12th, 2007, 07:17 AM
Authorized User
 
Join Date: Mar 2006
Location: , , .
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default Convering a String Array to an Integer array

Hi,
Is it possible to convert a string array which basically has no.'s to an integer array .. if so how ..?

------------------------------------------------------------------------------------------------------------
Adventure, Play, Alive, Fast, Race, Addict, React, Attack, Heat, Aggression, Blast, Ahead, Escape, Challenge, Anarchy, Game, Real, Damage, Insane, Acclerate,Passion, Fear, Dominate, Crazy, Awesome, Sweat, Habit, Avenge, Balls, Imagine, Viril, Chaos, Amuse, Heavy, Panic, @#!%, Fanatic
__________________
------------------------------------------------------------------------------------------------------------
<b><i>Adventure, Play, Alive, Fast, Race, Addict, React, Attack, Heat, Aggression, Blast, Ahead, Escape, Challenge, Anarchy, Game, Real, Damage, Insane, Acclerate,Passion, Fear, Dominate, Crazy, Awesome, Sweat, Habit, Avenge, Balls, Imagine, Viril, Chaos, Amuse, Heavy, Panic, @#!%, Fanatic </i></b>
Reply With Quote
  #2 (permalink)  
Old January 12th, 2007, 08:06 AM
Authorized User
 
Join Date: Mar 2006
Location: , , .
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Code:
for(int i=0;i<arrLen;i++)
    str = arInfo[i];
    index = Convert.ToInt32(str);
I'm getting the following exception-
Exception Details: System.FormatException: Input string was not in a correct format.
The arInfo[] is basically an array of numbers

------------------------------------------------------------------------------------------------------------
Adventure, Play, Alive, Fast, Race, Addict, React, Attack, Heat, Aggression, Blast, Ahead, Escape, Challenge, Anarchy, Game, Real, Damage, Insane, Acclerate,Passion, Fear, Dominate, Crazy, Awesome, Sweat, Habit, Avenge, Balls, Imagine, Viril, Chaos, Amuse, Heavy, Panic, @#!%, Fanatic
Reply With Quote
  #3 (permalink)  
Old January 12th, 2007, 04:52 PM
Registered User
 
Join Date: Dec 2006
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Please check this:

string[] arInfo = { "0", "1", "2", "3" };
int[] iarInfo = new int[4];
          for (int i = 0; i < arInfo.Length; i++)
          {
             iarInfo[i] = Convert.ToInt32(arInfo[i]);
             Console.Write(iarInfo[i]);
          }

i hope it will work for you.

Reply With Quote
  #4 (permalink)  
Old January 12th, 2007, 10:58 PM
Authorized User
 
Join Date: Mar 2006
Location: , , .
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I suppose that's the code that i used, and it doesn't work.

------------------------------------------------------------------------------------------------------------
Adventure, Play, Alive, Fast, Race, Addict, React, Attack, Heat, Aggression, Blast, Ahead, Escape, Challenge, Anarchy, Game, Real, Damage, Insane, Acclerate,Passion, Fear, Dominate, Crazy, Awesome, Sweat, Habit, Avenge, Balls, Imagine, Viril, Chaos, Amuse, Heavy, Panic, @#!%, Fanatic
Reply With Quote
  #5 (permalink)  
Old May 2nd, 2010, 06:07 PM
Registered User
 
Join Date: May 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The code works, just get sure that there is no "space" at the end of the last char in your string, if there is a space, then remove it.
Reply With Quote
  #6 (permalink)  
Old November 16th, 2010, 07:58 PM
Registered User
 
Join Date: Nov 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Extension Method Option

I just wanted to let you know that I have created a generic approach to this problem that allows you to specify the type you want the resulting array or list to be. A blog post describing this solution can be found here.
Reply With Quote
  #7 (permalink)  
Old November 17th, 2010, 03:24 AM
samjudson's Avatar
Friend of Wrox
Points: 8,436, Level: 39
Points: 8,436, Level: 39 Points: 8,436, Level: 39 Points: 8,436, Level: 39
Activity: 11%
Activity: 11% Activity: 11% Activity: 11%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,084
Thanks: 1
Thanked 180 Times in 179 Posts
Default

You use Generics. This is a C# 1.0 forum, therefore I would assume generics weren't allowed.

If they were allowed then just use the List<T>.ConvertAll() method instead.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
Reply With Quote
  #8 (permalink)  
Old November 17th, 2010, 09:47 AM
Registered User
 
Join Date: Nov 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I do apologize for posting in the incorrect forum. I didn't notice it was a C# 1.0 forum.

You propose and interesting suggestion. The only thing about the ConvertAll method is that it requires you to write a delegate that does the conversion from one type to another. First, that could separate the code more so you have to go looking for the converter logic (then again since we are out of the C# 1.0 realm, you could use an anonymous delegate which would place the code inline). Second, if you really want to make it generic like I did, you still have to use the Convert.ChangeType method or something similar as you can't directly cast a string to a decimal, float, etc and obviously you can throw in a XXX.Parse as that would be binding to a given type.

While ConvertAll is a great use of the framework, is there any real benefit of using it over what I did? Maybe this isn't the greatest place to continue this discussion but I would be interested in your feedback.

Thanks for you comment!

Nick
Reply With Quote
  #9 (permalink)  
Old November 17th, 2010, 10:57 AM
samjudson's Avatar
Friend of Wrox
Points: 8,436, Level: 39
Points: 8,436, Level: 39 Points: 8,436, Level: 39 Points: 8,436, Level: 39
Activity: 11%
Activity: 11% Activity: 11% Activity: 11%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,084
Thanks: 1
Thanked 180 Times in 179 Posts
Default

I guess it depends on how often you are going to be doing splits. Personally the code you have written to do it the generic way is great, but is much more code to write than simply doing it in line like this:

Code:
string theString = "1,2,3,4,5";
List<int> ints = theString.Split(',').ToList().ConvertAll<int>(s => Convert.ToInt32(s));
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
Reply With Quote
  #10 (permalink)  
Old November 17th, 2010, 11:02 AM
Registered User
 
Join Date: Nov 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

True true. Just like everything else in life, its conditional! You are right, if my goal wasn't re-usability and broad application the method you presented is a great one line solution. I think I will amend my post to include the solution you presented, citing you of course.

Thanks again.

Nick
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Go from 2d Array to 1d array without defining type OneQuestion General .NET 1 January 10th, 2008 10:13 AM
String to array ajit Javascript 5 January 20th, 2007 03:02 PM
String to Array Psion PHP How-To 1 September 5th, 2006 11:36 AM
Passing php array values to javascript array gkrishna Pro PHP 0 November 6th, 2004 02:20 AM
Instantiate Integer array in a stucture hashtlishnii VB.NET 2002/2003 Basics 2 June 18th, 2004 11:56 AM



All times are GMT -4. The time now is 12:22 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.