12 Replies - 998 Views - Last Post: 30 July 2015 - 04:25 AM Rate Topic: -----

#1 O'Niel  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 388
  • Joined: 13-September 15

List<string> item to List<int>

Posted 25 July 2015 - 12:14 PM

Hello,

I'm stuck somewhere, this is what I need:

I have a List<string> with for example the values:
Cat, Dog, 1, Car, Flower, 2, Website, Mouse, 3, Keyboard, Door, 4.
All the things above are strings. What I need to do now, is take the numbers (1, 2, 3, 4) and put them into another List, but a List of integers (List<int>).

This is the code I have now:
	
	List<string> db_values = db_content.Split(',').ToList();
			
	List<int> ids = new List<int>();
			
	int value_count = 0; 	//Take each Third element (ID) out of CSV-database
	foreach (string db_value in db_values) {
		if (value_count == 3) {
			Console.WriteLine(db_value);
			ids.Ad(Int32.Parse(db_value));
			value_count = 0;
		}
		value_count++;
	}


Mention that all the numbers (1, 2, 3, 4) Can be reached with jumps of three.

I get the error saying that I can't take a variable of a foreach-loop, I've also tried for loops with jumps of three, but that didn't worked out well.

Any way to do this?
Sorry, this is just my second day C#, have done other languages though.

Thanks for reading,
Niely

Is This A Good Question/Topic? 0
  • +

Replies To: List<string> item to List<int>

#2 andrewsw  Icon User is offline

  • the case is sol-ved
  • member icon

Reputation: 6375
  • View blog
  • Posts: 25,756
  • Joined: 12-December 12

Re: List<string> item to List<int>

Posted 25 July 2015 - 12:26 PM

What is the exact error message?
Was This Post Helpful? 1
  • +
  • -

#3 g4143  Icon User is offline

  • D.I.C Head

Reputation: 17
  • View blog
  • Posts: 52
  • Joined: 30-April 09

Re: List<string> item to List<int>

Posted 26 July 2015 - 01:48 AM

A easy C# way would be to use LINQ.

List<string> slist = new List<string>{
				"1", "2", "G4143", "Was", "345", "Again", "888"
			};

			var ilist = slist.Where (e => {int num; return int.TryParse(e, out num);}).Select (e => int.Parse(e)).ToList ();


Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is offline

  • the case is sol-ved
  • member icon

Reputation: 6375
  • View blog
  • Posts: 25,756
  • Joined: 12-December 12

Re: List<string> item to List<int>

Posted 26 July 2015 - 02:18 AM

I like LINQ. Here's another example:
    var ilist = from s in slist
                  where s.Length > 0 && s.All(char.IsDigit)
                  select int.Parse(s);

    foreach (int item in ilist) {
        Console.WriteLine(item.ToString());
    }

However, neither of these meet the OPs requirement of selecting every third element.

The basic problem is that the OP starts value_count at 0, so it attempts to read the fourth item as an integer. Changing this initial value to 1 fixes the code.
Was This Post Helpful? 0
  • +
  • -

#5 O'Niel  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 388
  • Joined: 13-September 15

Re: List<string> item to List<int>

Posted 26 July 2015 - 02:24 AM

View Postandrewsw, on 25 July 2015 - 12:26 PM, said:

What is the exact error message?


Hi!

This is the error I get:

Quote

test.cs(103,10): error CS1061: System.Collections.Generic.List<int> bevat geen
definitie voor Ad en er is geen extensiemethode Ad gevonden waarmee het
eerste argument van het type System.Collections.Generic.List<int> wordt
geaccepteerd (mogelijk ontbreekt er een gebruiksinstructie of een
assembly-verwijzing)


Translation to English:

Quote

test.cs: System.Collections.Generic.List<int> does not have a definition for Ad, and there's no extension method Ad found with which the first argument of the type System.Collections.Generic.List<int> is accepted with (probably an user instruction or assembly is missing)


This is the error while compiling.
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is offline

  • the case is sol-ved
  • member icon

Reputation: 6375
  • View blog
  • Posts: 25,756
  • Joined: 12-December 12

Re: List<string> item to List<int>

Posted 26 July 2015 - 02:39 AM

Add, not Ad. "does not have a definition for Ad" tells you this is the problem.

Another possible issue is the extra space before each item " Dog", etc.. Added: A quick check though and Parse ignores surrounding spaces (I'd prefer that it didn't).

This post has been edited by andrewsw: 26 July 2015 - 03:15 AM

Was This Post Helpful? 0
  • +
  • -

#7 andrewsw  Icon User is offline

  • the case is sol-ved
  • member icon

Reputation: 6375
  • View blog
  • Posts: 25,756
  • Joined: 12-December 12

Re: List<string> item to List<int>

Posted 26 July 2015 - 02:58 AM

To continue the LINQ examples:

Spoiler

Was This Post Helpful? 0
  • +
  • -

#8 O'Niel  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 388
  • Joined: 13-September 15

Re: List<string> item to List<int>

Posted 26 July 2015 - 03:38 AM

Could you explain this LINQ-code please?

I'm just going to use that one, but don't know how to exactly implement it.

var ilist = from s in slist
              where s.Length > 0 && s.All(char.IsDigit)
              select int.Parse(s);

foreach (int item in ilist) {
    Console.WriteLine(item.ToString());
}

Was This Post Helpful? 0
  • +
  • -

#9 andrewsw  Icon User is offline

  • the case is sol-ved
  • member icon

Reputation: 6375
  • View blog
  • Posts: 25,756
  • Joined: 12-December 12

Re: List<string> item to List<int>

Posted 26 July 2015 - 03:45 AM

That LINQ won't work with your list because it doesn't select every third item and the extra space before the numbers causes it to fail. The LINQ in my spoiler above does work with your list. Here it is again:
var ilist = from s in db_values
              let x = s.Trim()
              where (counter++ % 3 == 0) && x.Length > 0 && x.All(char.IsDigit)
              select int.Parse(x);

foreach (int item in ilist) {
    Console.WriteLine(item.ToString());
}



from s in db_values for each string in your list

let x = s.Trim() create a temporary (range) variable with leading and trailing whitespace removed

where (counter++ % 3 == 0) && x.Length > 0 && x.All(char.IsDigit) include the current item if it is the third item, is not an empty string, and all its characters are digits

select int.Parse(x); select the integer version of the current item.

To be honest though, your code works with the mentioned edits, so you have no reason to switch to LINQ, particularly if you don't fully understand it.
Was This Post Helpful? 0
  • +
  • -

#10 O'Niel  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 388
  • Joined: 13-September 15

Re: List<string> item to List<int>

Posted 26 July 2015 - 04:57 AM

Thanks,
I'll use your code to, because my one doesn't work. After successfully compiling and just testing it, it gives this error:

Quote

Onverwerkte uitzondering: System.FormatException: De indeling van de invoerteken
reeks is onjuist.
bij System.Number.StringToNumber(String str, NumberStyles options, NumberBuff
er& number, NumberFormatInfo info, Boolean parseDecimal)
bij System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo i
nfo)
bij insideFunctions.register()
bij mainClass.Main()

Was This Post Helpful? 0
  • +
  • -

#11 andrewsw  Icon User is offline

  • the case is sol-ved
  • member icon

Reputation: 6375
  • View blog
  • Posts: 25,756
  • Joined: 12-December 12

Re: List<string> item to List<int>

Posted 26 July 2015 - 05:03 AM

Ok.

@OP Ignore the following. It is just an example that may be of interest to others reading this topic.

I like playing with LINQ, so another way to do this (expanding on g4143 's example) is:
int[] ints = db_values
    .Where((x, i) => ((i+1) % 3 == 0) && x.Trim().All(char.IsDigit))
    .Select(x => int.Parse(x)).ToArray();

There is absolutely no advantage with this approach, it is just different; using LINQ extension methods rather than LINQ query syntax.

This also doesn't incorporate the check for an empty string. That would be:
int[] ints = db_values
    .Where((x, i) => ((i+1) % 3 == 0) && (x.Trim().Length > 0) && x.Trim().All(char.IsDigit))
    .Select(x => int.Parse(x)).ToArray();


This post has been edited by andrewsw: 26 July 2015 - 05:19 AM

Was This Post Helpful? 0
  • +
  • -

#12 g4143  Icon User is offline

  • D.I.C Head

Reputation: 17
  • View blog
  • Posts: 52
  • Joined: 30-April 09

Re: List<string> item to List<int>

Posted 26 July 2015 - 12:45 PM

LINQ is one of those technologies which works but most people don't recognize its significance.

LINQ is built on a functional design pattern called monads and monads are pretty cool. They(monads) plus immutable data allow for safer function composition and data manipulation. Notice how all LINQ transactions create a new data containers/sets and maintain the original. This is done to help produce immutable data and help produce pure transactions.

Note: C# isn't a pure functional language so you do have to take some care when using LINQ when you expect pure transactions and immutable data but its still cool.
Was This Post Helpful? 0
  • +
  • -

#13 O'Niel  Icon User is offline

  • D.I.C Regular

Reputation: 14
  • View blog
  • Posts: 388
  • Joined: 13-September 15

Re: List<string> item to List<int>

Posted 30 July 2015 - 04:25 AM

^Okay! Thanks for the extra explanation. :)
I've found the solution for this problem here, thanks all!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1