【问题标题】:Error "Expression does not produce a value" whenever I call my sub procedure每当我调用我的子过程时出现错误“表达式不产生值”
【发布时间】:2016-10-04 15:50:33
【问题描述】:

首先,我会说我是新来的,所以我可能无法正确解释我的问题。我使用 VB.Net Windows 窗体应用程序。在这个项目中,我必须从另一个类调用sub 过程,以便在单击按钮时显示报告。 sub 不返回我知道的值,而且我已经习惯了,每当我有 sub 时,我都会在 sub 中显示该值,但我不能在另一个中写入文本框的名称班级 。这些是我的代码,希望您能从中理解我的意思。

Public Class Form1
    Dim aobj As New Allowance
    Dim nam As String
    Dim ID, year, allowance, total As Double

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        allowance = txtamount.Text
        total = aobj.calculatetotal(allowance)
        txtcalculate.ForeColor = Color.Red
        txtcalculate.Text = FormatNumber(total)
    End Sub

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        ID = txtid.Text
        year = txtyear.Text
        nam = txtname.Text
        txtreport.ForeColor = Color.Blue
        txtreport.Text = aobj.displayreport(nam, ID, year, total)
    End Sub
End Class

Class Allowance

    Function calculatetotal(ByVal a As Double) As Double
        Return (12 * 5 * a)
    End Function

    Sub displayreport(ByVal nam As String, ByVal id As Double, ByVal y As Double, ByVal tot As Double)

         (" The Student " & nam & " with ID: " & id & " will receivce a total allowance of " & tot & " for five years of study in YUC")
    End Sub

End Class 

【问题讨论】:

  • 你有什么理由用 C# 标记这个?
  • displayreport 是一个 sub,你将 aobj.displayreport 分配给不可能的事情。同样在displayreport 内部,你只是有一个没有做任何事情的表达式(甚至没有分配给一些本地人)我认为这也会导致问题
  • 非常感谢。

标签: vb.net winforms


【解决方案1】:

如果要将字符串分配给 txtreport.Text,则需要将 displayreport 更改为函数并返回该字符串。尝试将其更改为:

Public Function displayreport(ByVal nam As String, ByVal id As Double, ByVal tot As Double) As String
    Return " The Student " & nam & " with ID: " & id & " will receivce a total allowance of " & tot & " for five years of study in YUC"
End Function

我还删除了“y as double”,因为它没有被使用

【讨论】:

  • 问题是我的老师要我使用一个子程序没有功能谢谢你的帮助。
  • 不幸的是,这样做的唯一方法比完成该任务所需的方法要复杂得多,但可以做到。您必须对调用代表、将文本框引用传递给您的配额类或向您的配额类添加一个公共属性进行一些研究,该属性将保存由您的 displayreport 子创建的字符串。当然,你必须为课堂作业做你必须做的事情,但如果你在现实世界中遇到过类似的问题,那么 sub 并不是最好的方法。
  • 谢谢这对我很有帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-01-17
  • 2016-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-10
  • 2016-11-10
相关资源
最近更新 更多