0

I'm having difficulty converting the below code from Java to C#.

        this.document.add(new VerticalPositionMark() {
            @Override
            public void draw(final PdfContentByte canvas, final float llx, final float lly, final float urx, final float ury, final float y)
            {
                final PdfTemplate createTemplate = canvas.createTemplate(50, 50);
                Main.this.tocPlaceholder.put(title, createTemplate);

                canvas.addTemplate(createTemplate, urx - 50, y);
            }
        });

I'm not really sure if it's possible to override on instantiation in C#. If there isn't, is there a way to replicate the code to achieve what's needed?

Display_Here
  • 311
  • 1
  • 10
  • 3
    That's an anonymous class; why not making a named class, and use that? – Elliott Frisch Jan 08 '15 at 16:05
  • You cannot implement any class anonymously in c#, at least not as you want it. You may have a look at this topic: http://stackoverflow.com/questions/24931211/anonymous-interface-implementation – MakePeaceGreatAgain Jan 08 '15 at 16:07
  • So should I write a new class object that inherits from `VerticalPositionMark`, and override the draw function then? – Display_Here Jan 08 '15 at 16:15
  • @user3643344 One would not be overriding the method as there is no base class with another implementation. It'd be a new implementation rather than an override. – Servy Jan 08 '15 at 16:22

1 Answers1

0

C# doesn't have anonymous classes like we know in Java (they can't extend other classes or implement interfaces). I would suggest you use Lambda expressions, but since you're working with a framework that's not an option.

Consider your starting Java code as the following, which extracts the anonymous inner class to a named class.

public class ContextClass
{
    public void ContextMethod()
    {
      this.document.add(new CustomVerticalPositionMark(title, this.tocPlaceholder));
    }
}

class CustomVerticalPositionMark extends VerticalPositionMark
{
    final String title;
    final PlaceHolder tocPlaceholder;

    CustomVerticalPositionMark(String title, PlaceHolder tocPlaceholder)
    {
        this.title = title;
        this.tocPlaceholder = tocPlaceholder;
    }

   @Override
   public void draw(final PdfContentByte canvas, final float llx, final float lly, final float urx, final float ury, final float y)
   {
       final PdfTemplate createTemplate = canvas.createTemplate(50, 50);
       tocPlaceholder.put(title, createTemplate);

       canvas.addTemplate(createTemplate, urx - 50, y);
   }
}
gknicker
  • 5,509
  • 2
  • 25
  • 41
  • The innerclass requires access to values of `title`(string within the same function call the object is being initialized) and `tocPlaceholder`. `draw()` is a an action event that's called when it's being added to document. – Display_Here Jan 08 '15 at 16:33
  • 1
    See my change, adds a constructor to the CustomVerticalPositionMark class. – gknicker Jan 08 '15 at 16:40
  • Side note: C# *does* have anonymous classes, they're just not used the same way Java's are. – Oblivious Sage Jan 08 '15 at 16:42
  • @ObliviousSage thanks, I corrected that statement in my answer – gknicker Jan 08 '15 at 16:48