簡體   English   中英

C#中的“方法必須具有返回類型”錯誤

[英]“Method must have a return type” error in c#

它說

“方法必須具有返回類型”

每當我嘗試調試它時。

我不知道如何在此行中修復此類,我有一個錯誤,

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Data.SqlClient;

    namespace WindowsFormsApplication1
    {
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //btn_Submit Click event

       Form1_Load(object sender, System.EventArgs e)
        {
            // Do whatever
        }


        private void button1_Click(object sender, EventArgs e)
        {
            string d, y, z;
            d = (textBox1.Text);
            y = (textBox2.Text);
            if (d == "" || y == "")
            {
                MessageBox.Show("ERROR");
                return;
            }
            try
            {
                //Create SqlConnection
                SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\MyDatabase.mdf;Integrated Security=True;");
                SqlCommand cmd = new SqlCommand("Select * from Table_1 where id=@d and password=@y", con);
                cmd.Parameters.AddWithValue("@username", d);
                cmd.Parameters.AddWithValue("@password", y);
                con.Open();
                SqlDataAdapter adapt = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                adapt.Fill(ds);
                con.Close();
                int count = ds.Tables[0].Rows.Count;
                //If count is equal to 1, than show frmMain form



                if (count == 1)
                {
                    MessageBox.Show("Login Successful!");
                    this.Hide();
                    frmMain fm = new frmMain();
                    fm.Show();
                }
                else
                {
                    MessageBox.Show("Login Failed!");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

    private void label1_Click_1(object sender, EventArgs e)
        {

        }

    }
}

我試圖更改為類名並添加了一些庫,但是我失敗了,我想我忘記了該類中的某些內容

有人可以幫我嗎?

更改

Form1_Load(object sender, System.EventArgs e)
{
    // Do whatever
}

void Form1_Load(object sender, System.EventArgs e)
{
    // Do whatever
}

您缺少方法簽名的重要部分,即返回類型。 由於該方法不應返回任何內容,因此請使用void

您的事件處理程序未指定任何返回類型,因此會出現錯誤。 由於它是事件處理程序,因此返回類型必須為void例如

private void Form1_Load(object sender, System.EventArgs e)
Form1_Load is handler which you subscribed to Load event of form. 

public event EventHandler Load;

因此,您的處理程序簽名應與委托EventHandler簽名匹配。 根據它應該是void

delegate void EventHandler(object sender, EventArgs e);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM