How to create two constructor overloads, taking only one string parameter?

For example, you want the object to possibly be initialized in two ways, using the file path and using the string. Typically, both constructors should take one string parameter, MyObject(string file) and MyObject(string content) , but overloading is not possible. What are you offering?

Edit: in the first case, the path to the file is also necessary, so please do not offer a solution that reads the contents of the file and simply passes the content to another constructor.

+6
constructor c #
source share
7 answers

Perhaps you could change the first so that instead of FileInfo :

 class MyObject { public MyObject(FileInfo file) { /* etc... */ } public MyObject(string content) { /* etc... */ } } ... MyObject o = new MyObject(new FileInfo(filename)); 
+10
source share

I am not a C # programmer, but it looks like work for the static factory method:

 class MyObject { public static MyObject FromContent(string content) { return new MyObject(content); } public static MyObject FromFile(string path) { return new MyObject(ReadContentFromFile(path)); } } 

Then you can do

 MyObject object = MyObject.FromFile("/some/path"); 

which is even more readable than using a regular constructor.

+15
source share

Or create factory methods:

 public static MyObject CreateByFilePath(string path){ ... } public static MyObject CreateByContent(string content){ ... } 
+1
source share

Perhaps you can use a factory?

 class MyObjectProvider { public static MyObject CreateByPath(string path) { return new MyObject { Path = path; }; } public static MyObject CreateByContent(string content) { return new MyObject { Content = content; }; } } 
+1
source share

I think you are referring to this question. ::

Constructor overload call when both overloads have the same signature

+1
source share
 public MyObject(Uri fileUri); public MyObject(string content); 
0
source share

I will take the opposite approach and say that do not do this. The best solutions published so far (use an alternative type, such as FileInfo or Uri for one of the overloads) seem a little hacky to me - they run counter to the principle of least surprise.

If you can use content only without a file name, this means that the file name is not significant. And similarly, if you can only build with a file name, then this is not necessary. Presumably, you can subsequently set the missing file name / content later, for example. setting the property:

 MyObject myObject = new MyObject(fileName); myObject.Content = ... MyObject myObject = new MyObject(content); myObject.FileName = ... 

Instead of trying to deal with it, select one of your options as the most important (file_name in the following example) and create two constructors as follows:

 public MyObject(string fileName) : this(fileName, null) { } public MyObject(string fileName, string content) { ... implementation } 

You can enable null for either or both parameters, if that makes sense. And you can insist that at least one of them is not null if necessary:

 public MyObject(string fileName, string content) { if (fileName == null && content == null) throw new ArgumentException(...); ... } 

In short, do not use hacks to circumvent restrictions like this.

0
source share

All Articles