vb.net 如何在vb.net中声明方法返回值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14934671/
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
how to declare method return value in vb.net
提问by Sahar Kiyan
I've made a class named Cls_ICCIDwhere I declare the method in Update_Statuswhich returns a byRef variable.
我创建了一个名为的类Cls_ICCID,我在Update_Status其中声明了返回 byRef 变量的方法。
Cls_ICCID
Cls_ICCID
Public Sub Update_Status(**ByRef massege As String**, ByVal ICCID_No As Integer, ByVal status As Integer)
Try
Dim cmd As SqlCommand
Dim sql As String
Dim myConnection As SqlConnection = New SqlConnection()
myConnection.ConnectionString = "Data Source=TEHRANI\TEHRANI;Initial Catalog=GSMProduction;Persist Security Info=True;User ID=sa;Password=1"
sql = "UPDATE Tbl_ICCID SET Status=status WHERE ICCID=ICCIDNo"
myConnection = New SqlConnection(sql)
myConnection.Open()
cmd = New SqlCommand(sql, myConnection)
cmd.ExecuteNonQuery()
cmd.Dispose()
myConnection.Close()
massege = "SeccessFully"
Catch ex As Exception
massege = "server Error"
End Try
End Sub
And then I execute that method when a textbox change event triggers:
然后在触发文本框更改事件时执行该方法:
Private Sub Txt_ICCID_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Txt_ICCID.TextChanged
Dim clsICCID As Cls_ICCID
clsICCID.Update_Status(lblError.Text, Txt_ICCID.Text, 1)
End Sub
But this gives me the exception:
但这给了我一个例外:
Arithmetic operation resulted in an overflow.
What am I doing wrong?
算术运算导致溢出。
我究竟做错了什么?
回答by Belial09
to be honest, as konrad said you should really work on the basics if you want to use such kind of code.
老实说,正如 konrad 所说,如果您想使用此类代码,您应该真正从事基础工作。
haven't tried the code out, but i think you want something like
还没有试过代码,但我想你想要类似的东西
Public Function Update_Status(iccidNo As Integer, status As Integer) As String
Const sql As String = "UPDATE Tbl_ICCID SET Status=@status WHERE ICCID=@ICCIDNo"
Const connstr As String = "Data Source=TEHRANI\TEHRANI;Initial Catalog=GSMProduction;Persist Security Info=True;User ID=sa;Password=1"
Try
Using myConnection = New SqlConnection()
myConnection.ConnectionString = connstr
Using cmd = New SqlCommand(sql, myConnection)
cmd.Parameters.AddWithValue("@status", status)
cmd.Parameters.AddWithValue("@ICCIDNo", iccidNo)
cmd.ExecuteNonQuery()
Update_Status = "Successfully"
End Using
End Using
Catch ex As SqlException
Update_Status = "server Error"
Catch ex As Exception
Update_Status = "server Error"
End Try
End Function
but personally i would handle the exceptions not that way and return a boolean.
但我个人不会那样处理异常并返回一个布尔值。
回答by SysDragon
The thing that you specifically ask is returning value in a method. Instead of Subyou must declare it like Functionand then return the value with return:
您特别要求的是在方法中返回值。而不是Sub您必须像这样声明它Function然后返回值return:
Public Function Update_Status(ICCID_No As Integer, status As Integer) As String
'...
return massege
End Function
And then catch the value:
然后捕获值:
lblError.Text = clsICCID.Update_Status(Txt_ICCID.Text, 1)
回答by shadow_map
For a method to return a value you need to declare your method as a function, instead of sub. For example:
对于返回值的方法,您需要将方法声明为函数,而不是子函数。例如:
Public Function Update_Status(ByVal ICCID_No As Integer, ByVal status As Integer) As String
Return "errorMessage"
End Function
And can then invoke it like this:
然后可以像这样调用它:
returnValue = Update_Status(Txt_ICCID.Text, 1)
see: http://msdn.microsoft.com/en-us/library/sect4ck6%28v=vs.80%29.aspx
请参阅:http: //msdn.microsoft.com/en-us/library/sect4ck6%28v=vs.80%29.aspx
But it also seems that your parameters might not match up: sending a String as an Integer?
但似乎您的参数可能不匹配:将字符串作为整数发送?
回答by Sahar Kiyan
I should use from Function instead of sub method. because sub method is void.
我应该使用 from Function 而不是 sub 方法。因为子方法是无效的。

What am I doing wrong?