Multiple constructors with complex logic

In C #, if you have multiple constructors, you can do something like this:

public MyClass(Guid inputId, string inputName){ // do something } public MyClass(Guid inputId): this(inputId, "foo") {} 

The idea, of course, is to reuse the code. However, what is the best approach when a little complex logic is required? Let's say I need this constructor:

 public MyClass(MyOtherClass inputObject) { Guid inputId = inputObject.ID; MyThirdClass mc = inputObject.CreateHelper(); string inputText = mc.Text; mc.Dispose(); // Need to call the main Constructor now with inputId and inputText } 

The caveat here is that I need to create an object that has , after use. (Clarification: not right away, but I have to call Dispose () and not wait for garbage collection)

However, I did not see a way to call the base constructor again if I add code inside my overloaded constructor. Is there a way to call the base constructor from overloaded?

Or you can use

 public MyClass(MyOtherClass inputObject): this(inputObject.ID, inputObject.CreateHelper().Text) {} 

Will it automatically remove the generated object from CreateHelper ()?

Edit: Thank you. Two problems: I do not control MyOtherClass, and I have no extension methods (only .NET 3.0 ...). However, I manage my own class, and since I just started writing it, I have no problem reorganizing the constructors if there is a good approach.

+6
c #
source share
3 answers

The most common pattern used to solve this problem is the Initialize () method, which your constructors will call, but in the example that you just gave, adding a static method that you called similar to the code below will do the trick.

 public MyClass(MyOtherClass inputObject): this(inputObject.ID, GetHelperText(inputObject) {} private static string GetHelperText(MyOtherClass o) { using (var helper = o.CreateHelper()) return helper.Text; } 
+17
source share

I see no reason to believe that creating an object in the constructor will automatically delete the object. Yes, your object will immediately go out of scope and will be available for garbage collection, but this is certainly not the same as the location.

There really isn't a great way to do exactly what you want to do, but it all seems like it can benefit from some refactoring. This usually takes place in my own code when I try to bend backwards to create constructor overloads.

If you have control over MyOtherClass, why not simplify access to this text property by adding a getter method that processes the utility:

 public class MyOtherClass { //... public string GetText() { using (var h = CreateHelper()) return h.Text; } } 

If you do not control MyOtherClass, you can use the extension method

 public static class MyOtherClassExtensions { public static string GetText(this MyOtherClass parent) { using(var helper = parent.CreateHelper()) { return helper.Text; } } } 

Then, of course, in your constructor you can safely call

 public MyClass(MyOtherClass inputObject): this(inputObject.ID, inputObject.GetText()) {} 
+2
source share

The object will be automatically deleted only when the garbage collection starts. If you want the utility to start as soon as it goes out of scope, you should use the using block:

 using (MyThirdClass mc = inputObject.CreateHelper()) { // do something with mc } 

This is really more of a style issue and doesn't really matter for your question.

+1
source share

All Articles