How to create two methods with the same signature
Using the example of
my previous post, we are going to extend the Folder entity.
- For a root folder, we must specify the drive in which this folder is.
- For a sub-folder, we must specify the folder in which this sub-folder is.
For the sake of this example, we will emulate typed DataSets (that is, not use OOP :D): This drive/folder is specified using its identifier (an integer, for example).
Therefore, we would like to create these two constructors:
public Folder(int driveId, string folderName) {...}
public Folder(int folderId, string subFolderName) {...}
However, this is not possible... (Do I need to explain why? :D)
So what is the solution?
I first thought about adding a "dummy" parameter, to have different signatures :D
After a quick "blushing", I reviewed other options:
- Changing the order of the parameters (More "blushing")
- Using a boolean (Bad! See previous post)
- Creating a constructor taking both identifiers (as nullable integers):
Folder rootFolder = new Folder(driveId, null, folderName);
Folder subFolder = new Folder(null, folderId, subFolderName);
I really don't like that.
I ended up with one constructor allowing to do this:
Folder rootFolder = new Folder(driveId, folderName, FolderType.Root);
Folder subFolder = new Folder(folderId, subFolderName, FolderType.Sub);
I am still a little bit annoyed with this solution because the first parameter has different meanings depending of the value of the third parameter... But I can't find a better one; can you?