Alice
Alice

Reputation: 183

Default value in method

I would like to have a method with default values.

Interface:

 public interface IMyFormatter
    {
        Bitmap Generate(string number, int width, int height);
    }

Concrete class:

 public class DefaultFormatter : IMyFormatter
    {

        private const int DEFAULT_BARCODE_WIDTH = 148;

        private const int DEFAULT_BARCODE_HEIGHT = 20;

        private const TYPE DEFAULT_BARCODE_TYPE = TYPE.CODE128;


        public Bitmap Generate(string Number, int Width = DEFAULT_BARCODE_WIDTH,
             int Height = DEFAULT_BARCODE_HEIGHT)
        {
            var zoneBarcode = new Barcode(Number, DEFAULT_BARCODE_TYPE);
            return new Bitmap(zoneBarcode.Encode(DEFAULT_BARCODE_TYPE, zoneBarcode.RawData, barcodeWidth, barcodeHeight));
        }

Usage:

var number = "1000";
var barcodeToPrint =_barcodeFormatter.Value.Generate(number);

I tried to modify interface like this (and adjust method in class to int? ):

public interface IMyFormatter
        {
            Bitmap Generate(string number, int? width = null, int? height = null);
        }

but values in my class are null, instead of default values and I get exception.

Upvotes: 0

Views: 184

Answers (2)

Martin Backasch
Martin Backasch

Reputation: 1899

There is a difference in calling the method of your interface or calling the method of you class.

So just as an example:

public interface IMyFormatter
{
    object Generate(string number, int? width = null, int? height = null);
}

public class DefaultFormatter : IMyFormatter
{
    private const int DEFAULT_BARCODE_WIDTH = 148;
    private const int DEFAULT_BARCODE_HEIGHT = 20;
    private const string DEFAULT_BARCODE_TYPE = "TYPE.CODE128";

    public object Generate(string Number, int? Width = DEFAULT_BARCODE_WIDTH, int? Height = DEFAULT_BARCODE_HEIGHT)
    {
        Console.WriteLine(Number);
        Console.WriteLine(Width);
        Console.WriteLine(Height);
        return new object();
    }
}

So when you now create a new object of your DefaultFormatter and call the Generate()

DefaultFormatter formatter = new DefaultFormatter();
formatter.Generate("100");

//Output: 100
//Output: 148
//Output: 20

but if you call it with the Interface type you will get the following Output:

((IMyFormatter) formatter).Generate("100");

 // Output: 100
 // Output: null
 // Output: null

You will get the same output when you create your class as the interface type.

IMyFormatter formatter= new DefaultFormatter();
formatter.Generate("100");

// Output: 100
// Output: null
// Output: null

Upvotes: 0

canton7
canton7

Reputation: 42360

Option 1: Put the default values in the interface. To do this, you will probably need to extract the constants out to another class, which can be accessed both from IMyFormatter and from DefaultFormatter (since interfaces can't have constants defined on them):

public static class FormatterDimensions
{
    public const int DEFAULT_BARCODE_WIDTH = 148;
    public const int DEFAULT_BARCODE_HEIGHT = 20;
}

public interface IMyFormatter
{
    Bitmap Generate(
        string number,
        int width = FormatterDimensions.DEFAULT_BARCODE_WIDTH,
        int height = FormatterDimensions.DEFAULT_BARCODE_HEIGHT);
}

Note that you will also need the default values in the method, in order to support some other CLR languages such as IronPython:

public class DefaultFormatter : IMyFormatter
{
    public Bitmap Generate(
        string number,
        int width = FormatterDimensions.DEFAULT_BARCODE_WIDTH,
        int height = FormatterDimensions.DEFAULT_BARCODE_HEIGHT)
    {

    }
}

Option 2: use int? instead. You'll need to do this both on the interface and the implementation:

public interface IMyFormatter
{
    Bitmap Generate(string number, int? width = null, int? height = null);
}

public class DefaultFormatter : IMyFormatter
{
    private const int DEFAULT_BARCODE_WIDTH = 148;
    private const int DEFAULT_BARCODE_HEIGHT = 20;

    public Bitmap Generate(string number, int? width = null, int? height = null)
    {
        int actualWidth = width ?? DEFAULT_BARCODE_WIDTH;
        int actualHeight = height ?? DEFAULT_BARCODE_HEIGHT;

        // Use actualWidth and actualHeight in your code
    }
}

Upvotes: 2

Related Questions