0

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?

1
  • 1
    if (line.Contains(pat) == true) could be changed to if (line.Contains(pat)) without loosing any meaning Commented Nov 11, 2011 at 14:03

6 Answers 6

4

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).

Sign up to request clarification or add additional context in comments.

2 Comments

thanks but now i get an other error.. An object reference is required for the non static field, method, por property (Function.SearchMethod([])
That's because you're calling SearchedRecords like a static method, but didn't declare it as one. To make it work as you've code you either need to create a new instance of your "Function" class, or else define the method like this: "public static int SearchedRecords(String [] recs)"
2

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++;
    }
}

Comments

1

Change it to

 int counter = Function.SearchedRecords(file);

1 Comment

thanks but now i get an other error.. An object reference is required for the non static field, method, por property (Function.SearchMethod([])
1

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

1 Comment

thanks but now i get an other error.. An object reference is required for the non static field, method, por property (Function.SearchMethod([])
1

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.

Comments

1

You can do this in a shorter way using LINQ:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.