VB.NET代码中的错误:“没有为参数指定参数”

时间:2011-08-25 10:30:13

标签: .net vb.net error-handling

当我在代码中放置Set_Symbol()时,会给我两个错误。

错误:

  

未指定'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

这就是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

这就是我所说的:

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

为什么会抛出此错误?

4 个答案:

答案 0 :(得分:2)

Set_Symbol()中的

frmindex_Load将无法编译,因为该方法需要两个参数,但您尝试在没有参数的情况下调用它。

您不应将事件处理程序代码与手动调用的代码混合,因为它们是两个不同的东西。如果你需要从事件处理程序调用方法以及从其他地方手动调用方法,你应该提供一个方法来获取适当的参数(如果不需要则不需要),并从两个位置调用它。

如果你真的想为每个RadioButton设置Tag,你应该提供一个不带参数的方法,并为每个RadioButton设置Tag属性。

我假设处理程序应该读取标记而不是设置它。

答案 1 :(得分:1)

您应该致电Set_Symbol(sender,e)进行编译

答案 2 :(得分:0)

请注意,Sub定义需要两个参数sendere,而您在Load期间未提供任何参数。幸运的是,如果你链接事件,你已经定义了sendere。因此,您可以直接处理Load事件,例如radiobutton操作,或使用两个参数调用它。

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

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

答案 3 :(得分:-1)

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