I have a custom class that I use to build table strings. I would like to rewrite the code so that I can chain the operators. Something like:
myObject
.addTableCellwithCheckbox("chkIsStudent", isUnchecked, isWithoutLabel)
.addTableCellwithTextbox("tbStudentName", isEditable) etc.
So it seems like I would have each of these methods(functions) return the object itself so that I can then call another method(function) on the resulting object, but I can’t figure out how to get a c# class to refer to itself.
Any help?
So you would create a fluent interface like this:
Now you could use it like this:
This should give you a basic idea of how you need to go about it. See fluent interfaces on wikipedia.
A more correct way to do it would be to implement a fluent interface:
And then implement it :
class FluentTable : IFluentTable {}