C
C#3y ago
Alex Frost

Pass DateTime.Add function as parameter

I have a switch statement that will call a generation function so I don't have to loop for every scenario:
switch (E.RepMode)
{
case "daily":
repeats.Add(GenerateRepeats(entry.DateStart, entry.DateEnd, 1, DateTime.AddDays));
break;
case "weekly":
break;
case "monthly":
break;
case "yearly":
break;
}
switch (E.RepMode)
{
case "daily":
repeats.Add(GenerateRepeats(entry.DateStart, entry.DateEnd, 1, DateTime.AddDays));
break;
case "weekly":
break;
case "monthly":
break;
case "yearly":
break;
}
The function needs to be something like this:
public List<EntryRepeat> GenerateRepeats(DateTime start, DateTime end, int interval, Func<double, DateTime> f)
{
do
{

}
while (true);
}
public List<EntryRepeat> GenerateRepeats(DateTime start, DateTime end, int interval, Func<double, DateTime> f)
{
do
{

}
while (true);
}
However I'm not able to pass the function being non-static. Is there a way to achieve this or a better approach?
5 Replies
Aaron
Aaron3y ago
just have (dt, added) => dt.AddDays(added), and then have Func<DateTime, double, DateTime> actually there isnt a function called AddDays spinthink
Pobiega
Pobiega3y ago
DateTime.AddDays(Double) Method (System)
Returns a new DateTime that adds the specified number of days to the value of this instance.
Aaron
Aaron3y ago
not sure what i was looking at earlier
Pobiega
Pobiega3y ago
But yeah, to answer @Alex Frost 's question, since you need a DateTime reference to "add days" to, just do what Aaron suggested and add it to the func
Alex Frost
Alex FrostOP3y ago
Thank you guys, I'll try that. My understanding of C# basic concepts is lacking but will revise.
Want results from more Discord servers?
Add your server