c# - How to refactor a class with overloaded constructors -


Text after "div class =" itemprop = "text">

I have a class with overloaded creator (C #) that can be started in some ways, and some parameters are optional The result - So there is a misleading bunch of manufacturers. New object (stra, STRB, ObjA) New object (stra, StGB, ObjB, STRC) New object (stra, STRB, New object (stra, STRB, stre, ObjB) new object (stra, STRB, stre, ObjA, and obsolete new object (stra, STRB, ObjB, STRC, StrD) STRC) New object (stra, STRB, stre, ObjB, STRC, StrD)

In case I To make a two-way correction, to hold the optional parameter,

create a structure
  new config (stra, STRB, strd, stre) New object (config, obja) new object (Config, ObjB, STRC)  

b) Optional parameter set as property

  A = new object (Obja) A.StrA = some; a. SRB = Something; a. STD = some; A tre = some;  

Which is the best way?

And such code is necessary restructuring - the last code maintainer says that "while IntelliSense used, the complexity of the manufacturer does not matter - it is always possible to check possible tips and select the right one "

Yes, I will do it again. IntelliSense is only very useful, and stares at the 5-Arif constructor, which appears to be 3 that tries to detect random strings, which means that any code is not readable to improve readability VB (or C # 4.0). ), I still go with the producer, but use the designated arguments. For C # 3.0, I will create a separate square to keep the initial information:

  class objectcitings {public string string {get; Set; } Public string strobe {get; Set; } ... public string ObjA {get; Set; } Public string ObjB {get; Set; }}  

and then make one of those arguments in the manufacturer:

  class object {public object (object setting settings); }  

and use the object initiator when using it:

  New object (new object settings {stra = ..., StrB = .. ., ObjA = ...})  

Its main advantage is in comparison to the properties in the object that this pattern guarantees that The object will be started as soon as it is built. With the properties of the object, it will be in an invalid state unless the customer sets them all correctly, and you have to validate it on every call.

This pattern is actually used in .NET FCL - see the XmlReader / XmlReaderSettings for its example.


Comments

Popular posts from this blog

c# - ListView onScroll event -

PHP - get image from byte array -

Linux Terminal Problem with Non-Canonical Terminal I/O app -