-6

I'm beginner C# programmer and I find myself a problem in a Windows Form Application that I'm working on Microsoft Visual C# 2010 Express. I have a button that opens a file dialog to select a specific Excel workbook and then retrieve the values of the first column and the fith column to a string like this:

string x = {"120,123,125,128,130,140,157,189,220,230,243,250"}

Then I've transformed this string and add to a list of string. Here is the code:

var y = string.Join(string.Empty, x).Split(',');
List<string> listX = new List<string>();
foreach (var x1 in y)
{
    listX.Add(x1);
}

The list format is like this:

listX1[0] = "120";
listX1[1] = "123";
listX1[2] = "125";

And so on. Now I have this method to count how many number are between, lets say, 123 and 150, which will give me the number of 5 (with 123 included). The method used is something like this:

public static int Count(IList<int> set, int min, int max)
{
      int count = 0;
      foreach (int i in set)
          if( i <= max && i >= min)
              count++
      return count;
}

Where min in this case would be 123 and max would be 150.

Now I know that I can't use this method because my list (listX1) is a list of strings. How can I manage to count the numbers between min and max, using C#? Anyone can help me with that?

Hope you can help me. Many Thanks.

user3664117
  • 49
  • 2
  • 11
  • 1
    It does not look like one string! – Sajeetharan May 22 '14 at 15:51
  • 9
    is this a homework assignment? – DMac the Destroyer May 22 '14 at 15:52
  • Do you mean you have a list ? – Baptiste Pernet May 22 '14 at 15:53
  • 1
    What have you tried with Count? It should be pretty straightforward, so add the code you wrote in your attempt and we'll correct it. – Pierre-Luc Pineault May 22 '14 at 15:54
  • @DMactheDestroyer, it does not matter if the question is homework or not, what matters is the amount of effort put into the post (which I agree is not much). – gunr2171 May 22 '14 at 15:54
  • @gunr2171 I'm not sure if this is the defacto opinion of SO anymore, but I asked because [I view homework questions differently than regular questions](http://meta.stackexchange.com/questions/10811/how-do-i-ask-and-answer-homework-questions). OP, if you try to put some extra work into your question with regards to this link, you'll get better results. And welcome to Stack Overflow! – DMac the Destroyer May 22 '14 at 16:07
  • OP, it appears that you edited your question significantly from where it was to begin with. Now you're showing your input being a list of integers rather than strings, and your first attempt being basically what Habib's answer suggests. Was this intentional? Are you still having issues? – StriplingWarrior May 22 '14 at 18:34

2 Answers2

5

I believe you have string array like:

string[] x = { "120", "123", "125", "128", "130", "140", "157", "189", "220", "230", "243", "250" };

You have to parse each item to int and then sort them, later you can get the count like:

int start = 123;
int end = 150;
var result = x.Select(int.Parse)
              .Count(r => r >= start && r < end);

If you want to use int.TryParse then you can do:

int start = 123;
int end = 150;
int temp;
var result = x.Select(r => { return int.TryParse(r, out temp) ? temp : -1; })
              .Count(r => r >= start && r < end);
Habib
  • 219,104
  • 29
  • 407
  • 436
0

This looks like an array of strings. You need to remove the double quotes to end up with an array of integers. From there, you can either loop through the array and count the numbers fitting in the interval or use a LINQ or Lambda expression

Tarik
  • 10,810
  • 2
  • 26
  • 40