|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Question about function parameter
Hi all. I have two functions A and B. Their functionality is listed below:
A does some processing stuff with the data A loops through the data and adds their values (say int) together A returns an int result B does some processing stuff with the data B loops through the data and multiplies their values (say int) together B returns an int result What i want to do is have one function and be able to specify what function to execute when that point reaches. Something like AB(sum) I know this is probably done with delegates but i am not sure how. thx |
|
#2
|
||||
|
||||
|
C# or VB?
Here's a VB version: Code:
Sub Test()
Dim data As New List(Of Integer)
data.Add(1)
data.Add(2)
data.Add(3)
data.Add(4)
Dim product As Integer = Aggregate(data, AddressOf mult)
Dim sum As Integer = Aggregate(data, AddressOf add)
MsgBox("Product: " & product & vbCrLf & "Sum: " & sum)
End Sub
Function Aggregate(ByVal data As IEnumerable(Of Integer), ByVal method As AggregateDelegate) As Integer
Return method(data)
End Function
Delegate Function AggregateDelegate(ByVal data As IEnumerable(Of Integer)) As Integer
Function add(ByVal data As IEnumerable(Of Integer)) As Integer
add = 0
For Each i As Integer In data
add += i
Next
End Function
Function mult(ByVal data As IEnumerable(Of Integer)) As Integer
mult = 1
For Each i As Integer In data
mult *= i
Next
End Function
__________________
Primary Forums: .Net Development, MS-SQL, C Programming VB.Net: It's not your father's Visual Basic. [Moving to ASP.Net] | [.Net Dos and Don't for VB6 Programmers] Last edited by f'lar : May 9th, 2008 at 08:15 AM. |
|
#3
|
|||
|
|||
|
...deleted by me...
Last edited by WebDunce : May 9th, 2008 at 10:22 AM. |
|
#4
|
|||
|
|||
|
...deleted by me...
Last edited by WebDunce : May 9th, 2008 at 10:22 AM. |
|
#5
|
|||
|
|||
|
Of course, another way is to have an add method and a multiply method call the common processing method.
Code:
private int[] PreProcessData(int[] data)
{
// do some basic processing to the data
// and return the modified data
}
public int AddThem(int[] data)
{
int[] modifiedData = PreProcessData(data);
int result = 0;
for (i = 0; i < modifiedData.Length; i++)
{
result += modifiedData[i];
}
return result;
}
public int MultiplyThem(int[] data)
{
int[] modifiedData = PreProcessData(data);
int result = 1;
for(int i = 0; i < modifiedData.Length; i++)
{
result *= modifiedData[i];
}
return result;
}
Now, whether you call AddThem() or MultiplyThem()...the common processing gets done. This is probably how I'd actually do it in a real program...but if you're doing an assignment you gotta do it however it was specified...I'm just showing options. Last edited by WebDunce : May 9th, 2008 at 10:20 AM. |
|
#6
|
|||
|
|||
|
It's overkill for your simple goal...but another way would be to create an abstract DataProcessor class that could be subclassed to add, multiply, divide, substract...or whatever...that could be passed into the processing method.
Below I demonstrate how an abstract class would work. Code:
// define the abstract class
public abstract class DataProcessor
{
// just need to define one abstract method
public abstract int GetResult(int[] data);
}
// define a class that adds
public class Adder : DataProcessor
{
// implement the abstract method
public override int GetResult(int[] data)
{
int result = 0;
for (int i = 0; i < data.Length; i++)
{
result += data[i];
}
return result;
}
}
// define a class that multiplies
public class Multiplier : DataProcessor
{
// implement the abstract method
public override int GetResult(int[] data)
{
int result = 1;
for (int i = 0; i < data.Length; i++)
{
result *= data[i];
}
return result;
}
}
Now...to see how the classes above would be used... Code:
// some random method that calls your
// desired method
void SomeMethod(int[] data)
{
// if you want to add
int sum = ProcessData(data, new Adder());
// if you want to multiply
int product = ProcessData(data, new Multiplier());
}
// this is the single method that does
// the processing as required
int ProcessData(int[] data, DataProcessor dataProcessor)
{
// do some general processing to
// modify the data
return dataProcessor.GetResult(data);
}
Now any object that subclasses DataProcessor can be an argument in the ProcessData method. Again, it's overkill for this problem, though. Last edited by WebDunce : May 9th, 2008 at 10:35 AM. |
|
#7
|
||||
|
||||
|
Lol, as long as we're going to town on this one, here's another delegate version that isolates the arithmetic operation, so you can easily do it as a lambda later if you want:
Code:
Sub Test()
Dim data As New List(Of Integer)
data.Add(1)
data.Add(2)
data.Add(3)
data.Add(4)
Dim product As Integer = Aggregate(data, AddressOf mult)
Dim sum As Integer = Aggregate(data, AddressOf add)
MsgBox("Product: " & product & vbCrLf & "Sum: " & sum)
End Sub
Function Aggregate(ByVal data As IEnumerable(Of Integer), ByVal method As AggregateDelegate) As Integer
Dim total As Integer
Dim started As Boolean = False
For Each i As Integer In data
If Not started Then
total = i
started = True
Else
total = method(total, i)
End If
Next i
Return total
End Function
Delegate Function AggregateDelegate(ByVal val1 As Integer, ByVal val2 As Integer) As Integer
Function add(ByVal val1 As Integer, ByVal val2 As Integer) As Integer
Return val1 + val2
End Function
Function mult(ByVal val1 As Integer, ByVal val2 As Integer) As Integer
Return val1 * val2
End Function
Notice that the test function didn't change, but if this were C# and wanted to use an anonymous function, the code would be much simpler. Last edited by f'lar : May 9th, 2008 at 11:34 AM. |
|
#8
|
|||
|
|||
|
Quote:
Thanks for all the answers here. Actually it is c sharp but i really dont want to make it as complex as creating new classes etc. The thing is that i have a set of 10 functions that do exactly the same but are different in the way they treat data (e.g. sum, subtract, multiply, divide, etc.). I really dont want to rewrite the function 10 times. I have done the pre-processor approach mentioned earlier but i really want to use some sort of a function pointer. |
|
#9
|
||||||||
|
||||||||
|
Using Delegates in C# Is Easy...
Using delegates in C# is easy; it involves 4 basic steps:
Last edited by WebDunce : May 11th, 2008 at 04:22 AM. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > .Net Development > Question about function parameter |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |