表达式在 vb.net 应用程序中不产生值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24862265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Expression does not produce a value in vb.net application
提问by Lamloumi Afif
I have a Winforms application, in which I'd like to use the MVVM design pattern :
我有一个 Winforms 应用程序,我想在其中使用 MVVM 设计模式:
I follow this tutorial
我按照这个教程
It is very interesting article, but i get this problem : My application is vb.net and I convert the code ( C#) to vb.net, it works fine except this one:
这是一篇非常有趣的文章,但我遇到了这个问题:我的应用程序是 vb.net,我将代码 (C#) 转换为 vb.net,它工作正常,除了这个:
C# code
C#代码
protected void ViewModel_Validated(object sender, EventArgs e)
{
this.ViewModel.AttachedControls.ToList().ForEach(c => this.errorProvider.SetError(c.Value as Control, ""));
if (!string.IsNullOrEmpty(this.ViewModel.Error)) {
this.ViewModel.Messages.ToList().ForEach(message => {
this.errorProvider.SetError(this.ViewModel.AttachedControls[message.Key] as Control, message.Value);
});
}
}
Vb.net code
vb.net 代码
Protected Sub ViewModel_Validated(ByVal sender As Object, ByVal e As EventArgs)
Me.ViewModel.AttachedControls.ToList().ForEach(Function(c) Me.errorProvider.SetError(TryCast(c.Value, Control), ""))
If Not String.IsNullOrEmpty(Me.ViewModel.[Error]) Then
Me.ViewModel.Messages.ToList().ForEach(Function(message)
Me.errorProvider.SetError(TryCast(Me.ViewModel.AttachedControls(message.Key), Control), message.Value)
End Function)
End If
End Sub
The problem is in this line:
问题出在这一行:
Me.ViewModel.AttachedControls.ToList().ForEach(Function(c) Me.errorProvider.SetError(TryCast(c.Value, Control), ""))
The Error:
错误:
Expression does not produce a value.
Expression does not produce a value.
I need to know
我需要知道
- What is the reason of this error?
- How can I fix it?
- 这个错误的原因是什么?
- 我该如何解决?
回答by Fabio
Change Functionto Sub.Functionmean method which return value, but your code: Me.errorProvider.SetError(TryCast(c.Value, Control), "")does not.
更改Function为Sub。Function返回值的意思是方法,但你的代码:Me.errorProvider.SetError(TryCast(c.Value, Control), "")没有。
From MSDN:
来自 MSDN:
To return a value to the calling code, use a Function procedure; otherwise, use a Sub procedure.
要将值返回给调用代码,请使用 Function 过程;否则,使用 Sub 过程。
So try:
所以尝试:
Me.ViewModel.AttachedControls.ToList().ForEach(Sub(c) Me.errorProvider.SetError(TryCast(c.Value, Control), ""))
And also next line you need to change:
还有下一行你需要改变:
Me.ViewModel.Messages.ToList().ForEach(Sub(message)
Me.errorProvider.SetError(TryCast(Me.ViewModel.AttachedControls(message.Key), Control), message.Value)
End Sub)
回答by Suji
In vb.net A sub and a function are both subroutines, or sections of code that can be called in the program. The difference between them is that a function has a return value and a sub does not. so it is better to change the function as sub to avoid the problem
在 vb.net 中, sub 和 function 都是子例程,或者可以在程序中调用的代码段。它们之间的区别在于函数有返回值而子函数没有。所以最好将函数更改为 sub 以避免出现问题

