c# 如何实现委托执行
我手头上有一个做项目的短程序,用vb.net写的,我现在没有能力翻译成c#,c#能力一般,主要是里面有几段代码不知道怎么改。具体如下:Solver.FunctionEva...
我手头上有一个做项目的短程序,用vb.net写的,我现在没有能力翻译成c#,c#能力一般,主要是里面有几段代码不知道怎么改。具体如下:
Solver.FunctionEvaluator = AddressOf FunctionValue//这段会回调FunctionValue这个函数。c#不知道怎么写,能力有限。
Dim Par As New Microsoft.SolverFoundation.Solvers.NelderMeadSolverParams
Dim Solution = Solver.Solve(Par)
Function FunctionValue(Model As Microsoft.SolverFoundation.Services.INonlinearModel, rowVid As Integer, Values As Microsoft.SolverFoundation.Services.ValuesByIndex, newValues As Boolean) As Object
Dim vTransX, vTransY, vAngle As Double
vTransX = Values(Model.GetIndexFromKey("xtrans"))
vTransY = Values(Model.GetIndexFromKey("ytrans"))
vAngle = Values(Model.GetIndexFromKey("angle"))
For i = 1 To vNumPins
'calculate new locations
aNewX(i) = aActualX(i) * Math.Cos(vAngle) - aActualY(i) * Math.Sin(vAngle) + vTransX
aNewY(i) = aActualX(i) * Math.Sin(vAngle) + aActualY(i) * Math.Cos(vAngle) + vTransY
'calculate distance from new locations to spec limits
aUSLX(i) = (aNomX(i) + aLTX(i)) - aNewX(i)
aOSLX(i) = aNewX(i) - (aNomX(i) + aUTX(i))
aUSLY(i) = (aNomY(i) + aLTY(i)) - aNewY(i)
aOSLY(i) = aNewY(i) - (aNomY(i) + aUTY(i))
Next i
'calculate maximum distance from new locations to spec limits
FunctionValue = -1000000000 'initialize value
For i = 1 To vNumPins
If aUSLX(i) > FunctionValue Then FunctionValue = aUSLX(i)
If aOSLX(i) > FunctionValue Then FunctionValue = aOSLX(i)
If aUSLY(i) > FunctionValue Then FunctionValue = aUSLY(i)
If aOSLY(i) > FunctionValue Then FunctionValue = aOSLY(i)
Next i
Return FunctionValue
End Function
各位高手帮忙看看,谢谢!
按照楼下说的,我在前面加了委托
public delegate double Functionvvv(INonlinearModel Model, int rowVid, ValuesByIndex Values, bool newValues,double dreturn);
然后在这段里面加
Functionvvv methodCall = Functionvalue;
Solver.FunctionEvaluator = methodCall.Invoke;
没有报错,但是在
NelderMeadSolverParams Par = new NelderMeadSolverParams();
ILinearSolution Solutions =(ILinearSolution)Solver.Solve(Par);//这段里面报错,我估计还是我的委托没写好,楼下的写法我没写成功,类型不知道怎么加进去,哎,能力有限,对自己无语!后面的委托函数(Functionvalue)没有执行。 展开
Solver.FunctionEvaluator = AddressOf FunctionValue//这段会回调FunctionValue这个函数。c#不知道怎么写,能力有限。
Dim Par As New Microsoft.SolverFoundation.Solvers.NelderMeadSolverParams
Dim Solution = Solver.Solve(Par)
Function FunctionValue(Model As Microsoft.SolverFoundation.Services.INonlinearModel, rowVid As Integer, Values As Microsoft.SolverFoundation.Services.ValuesByIndex, newValues As Boolean) As Object
Dim vTransX, vTransY, vAngle As Double
vTransX = Values(Model.GetIndexFromKey("xtrans"))
vTransY = Values(Model.GetIndexFromKey("ytrans"))
vAngle = Values(Model.GetIndexFromKey("angle"))
For i = 1 To vNumPins
'calculate new locations
aNewX(i) = aActualX(i) * Math.Cos(vAngle) - aActualY(i) * Math.Sin(vAngle) + vTransX
aNewY(i) = aActualX(i) * Math.Sin(vAngle) + aActualY(i) * Math.Cos(vAngle) + vTransY
'calculate distance from new locations to spec limits
aUSLX(i) = (aNomX(i) + aLTX(i)) - aNewX(i)
aOSLX(i) = aNewX(i) - (aNomX(i) + aUTX(i))
aUSLY(i) = (aNomY(i) + aLTY(i)) - aNewY(i)
aOSLY(i) = aNewY(i) - (aNomY(i) + aUTY(i))
Next i
'calculate maximum distance from new locations to spec limits
FunctionValue = -1000000000 'initialize value
For i = 1 To vNumPins
If aUSLX(i) > FunctionValue Then FunctionValue = aUSLX(i)
If aOSLX(i) > FunctionValue Then FunctionValue = aOSLX(i)
If aUSLY(i) > FunctionValue Then FunctionValue = aUSLY(i)
If aOSLY(i) > FunctionValue Then FunctionValue = aOSLY(i)
Next i
Return FunctionValue
End Function
各位高手帮忙看看,谢谢!
按照楼下说的,我在前面加了委托
public delegate double Functionvvv(INonlinearModel Model, int rowVid, ValuesByIndex Values, bool newValues,double dreturn);
然后在这段里面加
Functionvvv methodCall = Functionvalue;
Solver.FunctionEvaluator = methodCall.Invoke;
没有报错,但是在
NelderMeadSolverParams Par = new NelderMeadSolverParams();
ILinearSolution Solutions =(ILinearSolution)Solver.Solve(Par);//这段里面报错,我估计还是我的委托没写好,楼下的写法我没写成功,类型不知道怎么加进去,哎,能力有限,对自己无语!后面的委托函数(Functionvalue)没有执行。 展开
1个回答
展开全部
C# 声明个委托,再调用Invoke就行了
可以将委托指向一个写好的方法,也可以用lambda表达式动态声明委托。
Func<int> method=()=>Convert.ToInt32("5");
int result=method.Invoke();
可以将委托指向一个写好的方法,也可以用lambda表达式动态声明委托。
Func<int> method=()=>Convert.ToInt32("5");
int result=method.Invoke();
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询