The Dink Network

Reply to Re: Overthinking it

If you don't have an account, just leave the password field blank.
Username:
Password:
Subject:
Antispam: Enter Dink Smallwood's last name (surname) below.
Formatting: :) :( ;( :P ;) :D >( : :s :O evil cat blood
Bold font Italic font hyperlink Code tags
Message:
 
 
February 14th 2019, 05:02 PM
duck.gif
toof
Peasant He/Him
I disagree. 
Just a reminder, not that important, don't bang your heads too hard

I'll keep it simple, then build on (ignore potential issues). Suppose you have a class Human with two properties:
class Human
{
  public string Name {get; set;}
  public int Age {get; set;}
}

When I instantiate that class, I wanna do it by using lambda expression that will set parameters to call certain functions within that class, which determine whether the input (set) is acceptable or not... Phew

Again, keeping it simple, let's say that if someone wanna set Human's age to 137, he can't do that because there's a limit. I'm aware that the class has no implementation for that yet.
So the constructor should look like this:
var human = new Human(cfg =>
  cfg.Age.MaximumAge = 70,
  cfg.Age.MinimumAge = 0
);


The values of minimum and maximum age can be a field, or properties of a nested class within Human.

If I also wanna be able to implement some restrictions to Name property, I'll add a function that will be called when setting the Name property. Some regular expression that will filter out strings containing characters or numbers. Don't worry about the function itself, just how to get this lambda to work.

var human = new Human(cfg =>
  cfg.Age.MaximimumAge = 70,
  cfg.Age.MinimumAge = 0,
  cfg.Name.ContainSymbols = false,
  cfg.Name.ContainNumbers = true
);


Use nested classes, delegates, whatever... If this has no solution, the other thing I'll accept as a replacement is some class, say... Factory. So you can pass any generic class to it, and do the same as above.

var crap = new Factory<Human> (cfg =>
  cfg.Name.ContainSymbols = false
  // etc
);


I know this post is a bit messy. If anything remained unclear, ask... Bottom line, make a class where restrictions on property values are defined in a constructor, via lambda expression...