0
private void Extractions(string htmlFileName, int offLineFileNumber, bool onlineOffline)
        {
            if (onlineOffline == false)
            {
                OffLineDownload.offhtmlfiles();
                page = OffLineDownload.OffLineFiles[offLineFileNumber];
                byte[] bytes1 = File.ReadAllBytes(page);
                page = Encoding.GetEncoding(1255).GetString(bytes1);
                TextExtractor.GetDateTimeList(page);
                StreamWriter w = new StreamWriter(@"C:\Temp\" + htmlFileName);//@"d:\rotterhtml\rotterscoops.html");
                w.Write(page);
                w.Close();
                extractlinks.Links(@"C:\Temp\" + htmlFileName);
                TextExtractor.ExtractText(@"C:\Temp\" + htmlFileName, newText);
            }
            else
            {
                client.Encoding = System.Text.Encoding.GetEncoding(1255);
                page = client.DownloadString("http://rotter.net/scoopscache.html");
                TextExtractor.GetDateTimeList(page);
                StreamWriter w = new StreamWriter(@"d:\rotterhtml\rotterscoops.html");
                w.Write(page);
                w.Close();
                extractlinks.Links(@"d:\rotterhtml\rotterscoops.html");
                TextExtractor.ExtractText(@"d:\rotterhtml\rotterscoops.html", newText);
            }
        }

If the user calls the method I want him to have two options to call it with all 3 variables:

string htmlFileName, int offLineFileNumber, bool onlineOffline

Or to call it without the variables and if it's () so it will be automatic true.

Jeff
  • 739
  • 12
  • 31
  • 1
    There are various ways to achieve this. Overloaded methods or optional parameters. In the original version of C#, optional parameters were not supported and some think that overloaded methods are better, due to a clearer "contract" between the caller and the class. – Marius George Jun 27 '14 at 15:43

3 Answers3

2

You can use optional arguments to achieve what you want :

private void Extractions(string htmlFileName="", int offLineFileNumber=0, bool onlineOffline=true)
{
    // implementation
}

Then you can call your method with required parameters, something like :

Extractions("myfile.txt", 1, true);  // or
Extractions("myfile.txt", 1)        
Mukesh Modhvadiya
  • 2,178
  • 2
  • 27
  • 32
1

You can "overload" the method. Have the same name with multiple parameter signatures.

private void Extractions()
{

}

Since your method is a "void", I don't know what you mean by " if it's () so it will be automatic true."

Unless what you meant was:

private void Extractions(string htmlFileName, int offLineFileNumber)
{
     Extractions(htmlFileName, offLineFileNumber, true);
}

Of course you could also just make the offLine paremeter optional and give it a default of true.

private void Extractions(string htmlFileName, int offLineFileNumber, bool onlineOffLine = true)
{
     ....
} 
Bill Gregg
  • 7,067
  • 2
  • 22
  • 39
  • A method without parameters is a property. You could create a property called Extractions that is a boolean, that always returns false. But that would be confusing to me. Methods and Properties are different in fact and in meaning. – Bill Gregg Jun 27 '14 at 15:41
0

Yes, it's called method overloading.

Just define your method multiple times, with different parameters

private void Extractions(string htmlFileName, int offLineFileNumber, bool onlineOffline)
{
  //All your logic here
}

Overloaded method:

private void Extractions(string htmlFileName, int offLineFileNumber)
{
// Call the main method, passing true
Extractions(htmlFileName, offLineFileNumber, true);
}

See here: http://csharpindepth.com/Articles/General/Overloading.aspx

HaukurHaf
  • 13,522
  • 5
  • 44
  • 59