C# Methods – Is It Okay for a Class to Use Its Own Public Method?

cdesign-patternsmethodsobject-oriented

Background

I currently have a situation where I have an object that is both transmitted and received by a device. This message has several constructs, as follows:

public void ReverseData()
public void ScheduleTransmission()

The ScheduleTransmission method needs to call the ReverseData method whenever it is called. However, there are times where I will need to call ReverseData externally (and I should add outside the namespace entirely) from where the object is instantiated in the application.

As for the "receive" I mean that ReverseData will be called externally in an object_received event-handler to un-reverse the data.

Question

Is it generally acceptable for an object to call its own public methods?

Best Answer

I would say it's not only acceptable but encouraged especially if you plan to allow extensions. In order to support extensions to the class in C#, you would need to flag the method as virtual per the comments below. You might want to document this, however, so that someone isn't surprised when overriding ReverseData() changes the way ScheduleTransmission() works.

It really comes down to the design of the class. ReverseData() sounds like a fundamental behavior of your class. If you need to use this behavior in other places, you probably don't want to have other versions of it. You just need to be careful that you don't let details specific to ScheduleTransmission() leak into ReverseData(). That will create problems. But since you are already using this outside of the class, you probably have already thought that through.

Related Topic