【问题标题】:Expression does not produce a value in vb.net application表达式在 vb.net 应用程序中不产生值
【发布时间】:2014-09-11 18:59:19
【问题描述】:

我有一个 Winforms 应用程序,我想在其中使用 MVVM 设计模式:

我关注这个tutorial

这是一篇非常有趣的文章,但我遇到了这个问题:我的应用程序是 vb.net,我将代码 (C#) 转换为 vb.net,除了这个之外它工作正常:

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 代码

 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

问题出在这一行:

Me.ViewModel.AttachedControls.ToList().ForEach(Function(c) Me.errorProvider.SetError(TryCast(c.Value, Control), ""))

错误:

Expression does not produce a value.

我需要知道

  • 这个错误的原因是什么?
  • 我该如何解决?

【问题讨论】:

    标签: c# .net vb.net winforms mvvm


    【解决方案1】:

    Function 更改为Sub
    Function 表示返回值的方法,但您的代码:Me.errorProvider.SetError(TryCast(c.Value, Control), "") 没有。

    来自 MSDN:

    要向调用代码返回值,请使用 Function 过程; 否则,请使用 Sub 过程。

    那就试试吧:

    Me.ViewModel.AttachedControls.ToList().ForEach(Sub(c) Me.errorProvider.SetError(TryCast(c.Value, Control), ""))
    

    还有你需要更改的下一行:

    Me.ViewModel.Messages.ToList().ForEach(Sub(message)
                                               Me.errorProvider.SetError(TryCast(Me.ViewModel.AttachedControls(message.Key), Control), message.Value)
                                           End Sub)
    

    【讨论】:

      【解决方案2】:

      在 vb.net 子程序和函数都是子程序,或者可以在程序中调用的代码段。它们之间的区别在于函数有返回值而子函数没有。 所以最好把函数改成sub来避免这个问题

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-30
        • 1970-01-01
        • 2012-08-13
        • 2020-06-23
        • 2014-09-17
        相关资源
        最近更新 更多