Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

Looking for some advice on how to potentially achieve something, not sure if it is possible but I am sure someone somewhere will know the answer :)

I am trying to build a simple class but with one small complication, a property which will return a DATE value which is derived from a function. On the surface this seams simple but the complication arises as one of the properties of the class should allow a function to be declared on each instance of the class which will compute and generate a DATE value which can concatenated into a string to then be assigned to its target object.

The classes purpose is to provide 2 simple STRING properties for a name and key as well as a couple of BOOLEAN values as switches. The complication is that this class should allow a single INT16 value to be passed to the property/function which will then return a DATE. The calculation that is used to produce the DATE can be different in each instance of the class and I was wondering whether this was possible and if so, how?

Thanks in advance.
Posted

If I understand your question correctly that's easy. You could pass the function how the date should be calculated as a delegate to the constructor of your class. Example:
VB
Public Class Program
	Public Shared Sub Main()
		Dim ex1 As New Example(Function(v) DateTime.Now.AddDays(v))
		Dim ex2 As New Example(Function(v) DateTime.Now.AddYears(v))

		Console.WriteLine(ex1.CalculateDate(1))
		Console.WriteLine(ex1.CalculateDate(2))
		Console.WriteLine(ex2.CalculateDate(1))
		Console.WriteLine(ex2.CalculateDate(2))
	End Sub
End Class

Public Class Example
	Private ReadOnly DateCalculation As Func(Of Short, DateTime)

	Public Sub New(dateCalculation1 As Func(Of Short, DateTime))
		DateCalculation = dateCalculation1
	End Sub

	Public Function CalculateDate(val As Short) As DateTime
		Return DateCalculation(val)
	End Function
End Class


Console output:
5/13/2015 3:49:14 PM
5/14/2015 3:49:14 PM
5/12/2016 3:49:14 PM
5/12/2017 3:49:14 PM
 
Share this answer
 
Comments
PFerguson 12-May-15 12:18pm    
Absolutely perfect! Just could not get my head round this one and so simple and obvious now that I see it written down!
Thanks!!
Sascha Lefèvre 12-May-15 12:34pm    
You're welcome, glad I could help :)
If the calculation is different for each instance, then you might want to consider adding a Delegate[^] function to your class - the delegate can contain teh method to calculate the date, and it can be set to a different method for each instance as necessary.
This may help as well: A Beginner's Guide to Delegates[^]
 
Share this answer
 
Comments
PFerguson 12-May-15 12:20pm    
Thanks for the pointers and was just having a blank moment!
All sorted now :)
OriginalGriff 12-May-15 13:58pm    
We all have those! :laugh:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900