Delagates

07/09/2008 - 07:17 von Karsten Sosna | Report spam
Hallo NG,
Ich vesuche gerade eine neue Klasse zu entwerfen. Ich möchte gerne einen
Schönheitsfehler elimieren, hier ist erstmal die Klasse:
\\\
Public Class OperatorClass
Private _Name As String
Private _NumOfArguments As Integer
Private _Operation1 As Func(Of Double, Double)
Private _Operation2 As Func(Of Double, Double, Double)

Public Sub New(ByVal name As String, ByVal mathOperator As Func(Of
Double, Double))
_Name = name
_NumOfArguments = 1
_Operation1 = mathOperator
End Sub

Public Sub New(ByVal name As String, ByVal mathOperator As Func(Of
Double, Double, Double))
_Name = name
_NumOfArguments = 2
_Operation2 = mathOperator
End Sub

Public ReadOnly Property Name() As String
Get
Return _Name
End Get
End Property

Public ReadOnly Property NumOfArguments() As Integer
Get
Return _NumOfArguments
End Get
End Property

Public ReadOnly Property Operation1() As Func(Of Double, Double)
Get
Return _operation1
End Get
End Property

Public ReadOnly Property Operation2() As Func(Of Double, Double, Double)
Get
Return _operation2
End Get
End Property
End Class
///
Stören tun mich die beiden letzten Properties. Ich hàtte lieber nur ein
Property das sich dementsprechend verhàlt je nach dem welcher Delegate
zugewiesen wurde.Gilt natürlich auch für den Constructor. Also in etwa so:
\\\
Dim op1 As New OperatorClass("SIN", AddressOf Math.Sin)
result = op1.Operation.Invoke(x) 'Hier "Operation" anstelle von "Operation1"
Dim op2 As New OperatorClass("ATAN2", AddressOf Math.Atan2)
result = op2.Operation.Invoke(y, x) 'Hier "Operation" anstelle von
"Operation2"
///
Ich hoffe es ist verstàndlich geworden was ich meine.

Danke für jeden Tipp.
Gruß Scotty
 

Lesen sie die antworten

#1 Markus Schertler
07/09/2008 - 10:00 | Warnen spam
Hallo Karsten,

hatte eben eine Antwort gesendet. Irgendwie erscheint die nicht.
Sende daher nochmal falls die erste verlorengegangen ist.
Es ist gegenüber der ersten auch eine kleine Korrekur drin,
denn die Operation1 und Operation2 Properties werden ja auch innerhalb der
Klasse überflüssig.

Public Class Form1

Private Sub Form1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Click
Dim RESULT1 As Double
Dim RESULT2 As Double
Dim RESULT3 As Double
Dim RESULT4 As Double
Dim x As Double = 30
Dim y As Double = 50
Dim x1 As Double = 30
Dim y1 As Double = 50

op1 = New OperatorClass("SIN", AddressOf Math.Sin)
RESULT3 = op1.Operation(x) 'Hier "Operation" anstelle von "Operation1"

op2 = New OperatorClass("ATAN2", AddressOf Math.Atan2)
RESULT4 = op2.Operation(y, x) 'Hier "Operation" anstelle von
"Operation2"
End Sub


Public Class OperatorClass
Private _Name As String
Private _NumOfArguments As Integer
Private _Operation1 As Func(Of Double, Double)
Private _Operation2 As Func(Of Double, Double, Double)

Public Sub New(ByVal name As String, ByVal mathOperator As Func(Of
Double, Double))
_Name = name
_NumOfArguments = 1
_Operation1 = mathOperator
End Sub

Public Sub New(ByVal name As String, ByVal mathOperator As Func(Of
Double, Double, Double))
_Name = name
_NumOfArguments = 2
_Operation2 = mathOperator
End Sub

Public ReadOnly Property Name() As String
Get
Return _Name
End Get
End Property

Public ReadOnly Property NumOfArguments() As Integer
Get
Return _NumOfArguments
End Get
End Property

Public ReadOnly Property Operation(ByVal x As Double) As Double
Get
Return _Operation1.Invoke(x)
End Get
End Property

Public ReadOnly Property Operation(ByVal y As Double, ByVal x As Double)
As Double
Get
Return _Operation2.Invoke(y, x)
End Get
End Property


End Class

End Class

Ähnliche fragen