Click here to Skip to main content
15,887,746 members
Home / Discussions / C#
   

C#

 
GeneralRe: Select element from a List<> Pin
BillWoodruff5-Apr-15 10:12
professionalBillWoodruff5-Apr-15 10:12 
GeneralRe: Select element from a List<> Pin
DPaul19945-Apr-15 10:16
DPaul19945-Apr-15 10:16 
SuggestionRe: Select element from a List<> Pin
Richard Deeming7-Apr-15 2:21
mveRichard Deeming7-Apr-15 2:21 
GeneralRe: Select element from a List<> Pin
DPaul19947-Apr-15 6:43
DPaul19947-Apr-15 6:43 
Questionneed to order a file in stream read and write c# Pin
Member 115700984-Apr-15 7:31
Member 115700984-Apr-15 7:31 
AnswerRe: need to order a file in stream read and write c# Pin
PIEBALDconsult4-Apr-15 8:03
mvePIEBALDconsult4-Apr-15 8:03 
GeneralRe: need to order a file in stream read and write c# Pin
Member 115700984-Apr-15 8:26
Member 115700984-Apr-15 8:26 
QuestionC# class constructor best practice ? Pin
BillWoodruff4-Apr-15 5:05
professionalBillWoodruff4-Apr-15 5:05 
Assume you have a Class named 'TheClass : here's what you might call a 'normal' ctor:
C#
public class TheClass
{
    public string Name { private set; get; }

    public List<int> TheClassList { private set; get; }

    public TheClass(string name, int init, int inc, int last)
    {
        Name = name;

        TheClassList = new List<int>;

        for (int i = init; i < last; i+= inc)
        {
           TheClassList.Add(i);
        }   
    } 
}
Now you decide that you want to give the user the option to inject a function into ctor that will modify the generation of values in the 'for loop: so you write a Function:
C#
public Func<int, int> Generator1 = (value) => (value * value);
And, you modify the class to pass the Function as a parameter to the ctor, and use it:
C#
public class TheClass
{
    public string Name { private set; get; }

    public List<int> TheClassList { private set; get; }

    public TheClass(string name, int init, int inc, int last, Func<int,int> Generator = null)
    {
        Name = name;

        TheClassList = new List<int>;

        if(Generator == null)
        {
            for (int i = init; i < last; i+= inc)
            {
               TheClassList.Add(i);
            }   
        }
        else
        {
            for (int i = init; i < last; i+= inc)
            {
               TheClassList.Add(Generator(i));
            }   
        }
    } 
}
All this is, imho, passable; my question regards what happens when you have many optional types of Functions (or Actions) that you want to be used in the ctor ... so that if you made all of those Funcs (or Actions) part of the ctor parameter list (with 'null as their default value) you'd get a very unwieldy ctor, and/or complex logic in the ctor to decide which functions passed as parameters are invoked.

Here's what I see as the alternatives:

I. define a separate ctor for each possible use case (my current choice).
C#
public TheClass(string name, int init, int inc, int last); // default initialization

public TheClass(string name, int init, int inc, int last, Func<int,int> Generator1); // initialization passing one type of Func

public TheClass(string name, int init, int inc, int last, Func<int,int,int, List<int>> Generator2); // initialization passing another type of Func

II. break-out the code in the ctor in two parts: one that handles everything but the generation of values; put the code for all possible value generation scenarios (and its logic) into a separate 'Initialize function that must be called after the call to the ctor.

III. define the most common (default) initialization method in the ctor, and pass a boolean flag as a parameter to the ctor: if the flag is true, initialization is performed in the ctor, else defer initialization to a call to an 'Initialize function.

Appreciate your thoughts !
«To kill an error's as good a service, sometimes better than, establishing new truth or fact.» Charles Darwin in "Prospero's Precepts"


modified 4-Apr-15 12:58pm.

GeneralRe: C# class constructor best practice ? Pin
PIEBALDconsult4-Apr-15 5:52
mvePIEBALDconsult4-Apr-15 5:52 
GeneralRe: C# class constructor best practice ? Pin
BillWoodruff4-Apr-15 8:21
professionalBillWoodruff4-Apr-15 8:21 
GeneralRe: C# class constructor best practice ? Pin
PIEBALDconsult4-Apr-15 8:27
mvePIEBALDconsult4-Apr-15 8:27 
AnswerRe: C# class constructor best practice ? Pin
Eddy Vluggen4-Apr-15 6:47
professionalEddy Vluggen4-Apr-15 6:47 
GeneralRe: C# class constructor best practice ? Pin
BillWoodruff4-Apr-15 8:28
professionalBillWoodruff4-Apr-15 8:28 
GeneralRe: C# class constructor best practice ? Pin
PIEBALDconsult4-Apr-15 8:50
mvePIEBALDconsult4-Apr-15 8:50 
GeneralRe: C# class constructor best practice ? Pin
Eddy Vluggen4-Apr-15 11:13
professionalEddy Vluggen4-Apr-15 11:13 
GeneralRe: C# class constructor best practice ? Pin
BillWoodruff4-Apr-15 21:42
professionalBillWoodruff4-Apr-15 21:42 
GeneralRe: C# class constructor best practice ? Pin
Eddy Vluggen5-Apr-15 13:13
professionalEddy Vluggen5-Apr-15 13:13 
QuestionIs there any solution to design report instead of Crystal? Pin
aahamdan4-Apr-15 4:14
aahamdan4-Apr-15 4:14 
AnswerRe: Is there any solution to design report instead of Crystal? Pin
OriginalGriff4-Apr-15 4:44
mveOriginalGriff4-Apr-15 4:44 
AnswerRe: Is there any solution to design report instead of Crystal? Pin
tecnova14-Apr-15 4:58
professionaltecnova14-Apr-15 4:58 
GeneralRe: Is there any solution to design report instead of Crystal? Pin
aahamdan4-Apr-15 10:14
aahamdan4-Apr-15 10:14 
GeneralRe: Is there any solution to design report instead of Crystal? Pin
tecnova18-Apr-15 14:54
professionaltecnova18-Apr-15 14:54 
AnswerRe: Is there any solution to design report instead of Crystal? Pin
Eddy Vluggen4-Apr-15 6:50
professionalEddy Vluggen4-Apr-15 6:50 
GeneralRe: Is there any solution to design report instead of Crystal? Pin
Mycroft Holmes5-Apr-15 14:35
professionalMycroft Holmes5-Apr-15 14:35 
GeneralRe: Is there any solution to design report instead of Crystal? Pin
Eddy Vluggen6-Apr-15 0:33
professionalEddy Vluggen6-Apr-15 0:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.