具有动态功能的VB

具有动态功能的VB

本文介绍了具有动态功能的VB.NET类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

寻找一些有关如何潜在实现目标的建议,不确定是否可能,但我相信某个地方的人会知道答案的:)

我正在尝试构建一个简单的类,但具有一个小的复杂性,即一个将返回从函数派生的DATE值的属性.从表面上看,这种接缝很简单,但是由于类的属性之一,应该允许在类的每个实例上声明一个函数,从而引起复杂性,该函数将计算并生成一个DATE值,该值可以连接到字符串中,然后分配给该字符串.它的目标对象.

该类的目的是为名称 提供2个简单的STRING属性,以及两个BOOLEAN值作为开关.复杂的是,此类应允许将单个INT16值传递给属性/函​​数,然后该属性/函数将返回DATE.在类的每个实例中,用于生成DATE的计算可能会有所不同,我想知道这是否可能,如果可以,怎么办?

在此先感谢您.

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.

推荐答案

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



这篇关于具有动态功能的VB.NET类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 06:05