tequilaras
tequilaras

Reputation: 279

send a String array as parameter to a function

I have a function in a class called Function, like below:

public int SearchedRecords(String [] recs)
{
    int counter = 0;
    String pat = "-----";
    String[] records = recs;

    foreach (String line in records)
    {
        if (line.Contains(pat) == true)
        {
            counter++;
        }
    }

    return counter;
}

And I am calling this method from my main class this way:

        String [] file = File.ReadAllLines("C:/Users.../results.txt");
        int counter = Function.SearchedRecords( []file);

But I get an error saying:

;expected

What is wrong?


Another question: The function above is counting from a file all the lines with the pattern ----- in them (even if with more dashes, or if the line has some chars before or after the dashes). Am I right?

It's something like the patterns in Java so maybe there is an other way.
Can you enlighten me?

Upvotes: 0

Views: 7040

Answers (6)

Adrian Fâciu
Adrian Fâciu

Reputation: 12552

You can do this in a shorter way using LINQ:

 int counter = file.Count(line => line.Contains("-----"));

Upvotes: 1

Pankaj Upadhyay
Pankaj Upadhyay

Reputation: 13574

First of all you need to create an instance of function class and then run the function. Hope following code helps

Function fb = new Function();
int counter = fb.SearchedRecords(file);

Right now, you are using SearchRecords as an static function of a static class which doesn't require instantiation.

Upvotes: 1

Binary Worrier
Binary Worrier

Reputation: 51711

Change

int counter = Function.SearchedRecords( []file);

to

int counter = Function.SearchedRecords(file);

and yes, this will work, for that string. However Contains is case sensitive, if you were matching on a name, or another string with alphabetic characters, the case would have to be identical to match e.g. line.Contains("Binary Worrier") will not match a string "Hello binary worrier".

Also, reading the entire file into memory is fine if you know that the file will always be small, this method gets less efficient the larger the file. Better to always use something like System.IO.StreamReader or System.IO.File.ReadLines (available in .Net 4 and later), these allow you to consume the file one line at a time. e.g.

using (var reader = new System.IO.StreamReader("MyFile.txt"))
{
    while(!reader.EndOfStream)
    {
        string line = reader.ReadLine();
        if (line.Contains(pattern))
            counter++;
    }
}

Upvotes: 2

Andrey Atapin
Andrey Atapin

Reputation: 7945

Remove '[]' from a method call. Yes, your function seems to count what you want.

Upvotes: 1

Andrew Anderson
Andrew Anderson

Reputation: 3449

Remove the [] from your parameter.

e.g.

int counter = Function.SearchedRecords(file);

And yes, your assumption about the behavior of the Contains method is correct - you'll match any line containing five consecutive dashes, regardless of what characters are before or after them.

If you want to parse for exactly five dashes, with nothing before or after them I suggest looking into the RegEx class (regular expressions).

Upvotes: 4

Muthu
Muthu

Reputation: 2685

Change it to

 int counter = Function.SearchedRecords(file);

Upvotes: 1

Related Questions