VB.NET 代码中的错误:“未为参数指定参数”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7188814/
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
Error in VB.NET code: "Argument not specified for parameter"
提问by Kuzon
When I place Set_Symbol()in my code it give me two errors.
当我放入Set_Symbol()代码时,它给了我两个错误。
Errors:
错误:
Argument not specified for parameter 'e' of 'Private Sub Set_Symbol(sender As Object, e As System.EventArgs)'. d:\documents\visual studio 2010\Projects\Math Game\Math Game\frmindex.vb
Argument not specified for parameter 'sender' of 'Private Sub Set_Symbol(sender As Object, e As System.EventArgs)'. d:\documents\visual studio 2010\Projects\Math Game\Math Game\frmindex.vb
未为“Private Sub Set_Symbol(sender As Object, e As System.EventArgs)”的参数“e”指定参数。d:\documents\visual studio 2010\Projects\Math Game\Math Game\frmindex.vb
未为“Private Sub Set_Symbol(sender As Object, e As System.EventArgs)”的参数“sender”指定参数。d:\documents\visual studio 2010\Projects\Math Game\Math Game\frmindex.vb
This is what Set_Symbol is:
这就是 Set_Symbol:
Private Sub Set_Symbol(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles rbnaddition.Click, rbnsubtraction.Click, rbnmultiplication.Click, rbndivision.Click
Dim rbn As RadioButton
rbn = CType(sender, RadioButton)
symbol = rbn.Tag
End Sub
This is how I called it:
我是这样称呼它的:
Private Sub frmindex_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
symbolrbn(0) = rbnaddition
symbolrbn(1) = rbnsubtraction
symbolrbn(2) = rbnmultiplication
symbolrbn(3) = rbndivision
Set_Symbol()
Populate()
End Sub
Why is it throwing this error?
为什么会抛出这个错误?
回答by Tim Schmelter
Set_Symbol()in frmindex_Loadwon't compile because the method takes two parameters but you are trying to call it without parameters.
Set_Symbol()infrmindex_Load不会编译,因为该方法需要两个参数,但您试图在没有参数的情况下调用它。
You shouldn't mix event-handler code with code that is manually called because they are two different things. If you need to call a method from an event-handler as well as manually from somewhere else, you should provide a method that takes the appropriate parameters(or none if not needed) and call that from both locations.
您不应该将事件处理程序代码与手动调用的代码混合在一起,因为它们是两种不同的东西。如果您需要从事件处理程序以及从其他地方手动调用一个方法,您应该提供一个方法,该方法采用适当的参数(如果不需要,则没有)并从两个位置调用该方法。
If you actually wanted to set the Tagfor each RadioButton, you should provide a method that takes no parameter and sets the Tag property for each RadioButton.
如果您确实想Tag为每个 RadioButton设置,您应该提供一个不带参数并为每个 RadioButton 设置 Tag 属性的方法。
I assuming that the handler should read the Tag instead of set it.
我假设处理程序应该读取标签而不是设置它。
回答by Ignacio Soler Garcia
You should call Set_Symbol(sender,e)to make it compile
您应该调用Set_Symbol(sender,e)以使其编译
回答by ja72
Notice the Subdefinition requires two parameters senderand eand you have not supplied it with any during Load. Luckily you already have a senderand edefined, if you chain the event. So you could directly handle the Loadevent like the radiobutton actions, or call it with the two parameters.
请注意,该Sub定义需要两个参数sender,e并且您没有为它提供任何 during Load。幸运的是,如果您将事件链接起来,您已经定义了sender和e。因此,您可以Load像单选按钮操作一样直接处理事件,或者使用两个参数调用它。
Private Sub Set_Symbol(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles rbnaddition.Click, rbnsubtraction.Click, rbnmultiplication.Click, _
rbndivision.Click, MyBase.Load
Dim rbn As RadioButton
rbn = CType(sender, RadioButton)
If rbn IsNot Nothing then
symbol = rbn.Tag
End If
End Sub
or
或者
Private Sub frmindex_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles MyBase.Load
symbolrbn(0) = rbnaddition
symbolrbn(1) = rbnsubtraction
symbolrbn(2) = rbnmultiplication
symbolrbn(3) = rbndivision
Set_Symbol(rbnaddition, Nothing) 'Default to '+' symbol
Populate()
End Sub
回答by karishma
Imports System.Data.SqlClient
Public Class Form1
'Dim con As New SqlConnection("Data Source=admin-pc\sqlexpress;Initial Catalog=employee;Integrated Security=True")
Dim con As SqlConnection
Dim cmd As SqlCommand
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
con = New SqlConnection("Data Source=admin-pc\sqlexpress;Initial Catalog=employee;Integrated Security=True")
con.Open()
cmd = New SqlCommand("insert into emp_table values('" &TextBox1.Text "')",con)
cmd.ExecuteNonQuery()
con.Close()
End Sub
End Class

