1

谁能告诉我为什么当我尝试声明和使用“public void”函数时,它给了我错误:

 Expected class, delegate, enum, interface, or struct

我一开始就声明了它并正确设置了它,它不会调用我的主体。我已经研究过了,这似乎是可行的。

编辑:

 public void receipt();

 namespace ConsoleApp
 {
      class Progam
      {
           static ... Main()
           {
                ...
           }
      }
 }

 public void receipt()
 {
      ...
 }

所以它需要在“类程序”大括号中?

4

5 回答 5

7

您必须声明包含在类或结构中的方法,因为方法不是根成员。

于 2012-09-04T08:26:07.433 回答
1

我假设您正在尝试声明一个不包含类(或结构)主体的函数。请注意,在 C# 中,每个方法都必须在类中声明。

请注意,如果您不想创建一个能够调用该方法的对象,您可以将其声明为“静态”,如下所示:


public class MyClass
{
public static void MyMethod()
{
    Console.WriteLine("Hello World from static method");
}
}

您可以轻松使用:

MyClass.MyMethod();

在你的情况下:

公共无效收据();// C#中没有前向声明

命名空间 ConsoleApp { 类程序 { 静态 ... Main() { ... } } }

public void receipt() // 这需要在类中声明 { ... }

工作 C# 代码是:


 namespace ConsoleApp
 {
      class Progam
      {
           static ... Main()
           {
                Program program = new Program();
                program.receipt();
                // or static method
                Program.receipt_static(); 

           }
           public static void receipt_static()
           {
            ...
           }
      }

   public void receipt()
  { ... }
 }
}

于 2012-09-04T08:26:29.267 回答
1

放入public void receipt()一个类(在 Program 或一个新类中)并删除public void receipt();.

于 2012-09-04T09:58:22.610 回答
0

从错误看来,您似乎缺少类声明。

你确定你有这样的东西:

public class Foo
{
     public void Bar()
     {
          ...
     }
}
于 2012-09-04T08:27:07.787 回答
0
    private void btnBrowse_Click(object sender, EventArgs e)
    {
        try
        {
            // Create an instance of the open file dialog box.
            OpenFileDialog fld = new OpenFileDialog();

            // Set filter options and filter index.
            fld.Filter = "CSV Files (.CSV) |*.csv*";
            fld.FilterIndex = 1;

            fld.Multiselect = false;

            // Call the ShowDialog method to show the dialog box.
            if (fld.ShowDialog() == DialogResult.OK)
            {
                txtBrowse.Text = fld.FileName;
            }
            fld = null;
        }
        catch (Exception ex)
        {

            MessageBox.Show(ex.Message.ToString(), "CSV Browse", MessageBoxButtons.OK, MessageBoxIcon.Error); 
        }

    }

    private void btnReadCSV_Click(object sender, EventArgs e)
    {
        try
        {
            DataTable dt = GetDataTableFromCsv(txtBrowse.Text, chkHasHeader.Checked);
            grvData.DataSource = dt;
        }
        catch (Exception ex)
        {

            MessageBox.Show(ex.Message.ToString(), "CSV Read", MessageBoxButtons.OK, MessageBoxIcon.Error); 
        }

    }

    static DataTable GetDataTableFromCsv(string path, bool isFirstRowHeader)
    {
        string header = isFirstRowHeader ? "Yes" : "No";

        string pathOnly = Path.GetDirectoryName(path);
        string fileName = Path.GetFileName(path);

        string sql = @"SELECT * FROM [" + fileName + "]";

        using (OleDbConnection connection = new OleDbConnection(
                  @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + pathOnly +
                  ";Extended Properties=\"Text;HDR=" + header + "\""))
        using (OleDbCommand command = new OleDbCommand(sql, connection))
        using (OleDbDataAdapter adapter = new OleDbDataAdapter(command))
        {
            DataTable dataTable = new DataTable();
            dataTable.Locale = CultureInfo.CurrentCulture;
            adapter.Fill(dataTable);
            return dataTable;
        }
    }

    private void btnImport_Click(object sender, EventArgs e)
    {
        try
        {

        DataTable dt = grvData.DataSource as System.Data.DataTable; //Getting data from Datagrid

        string strSQL = "create Table " + txtTabelName.Text + " (";

        foreach (DataColumn dc in dt.Columns)
        {
            if (dc.DataType.ToString() == "System.String")
            {
                strSQL += dc.ColumnName + " varchar(255), ";
            }
            else if (dc.DataType.ToString() == "System.Double")
            {
                strSQL += dc.ColumnName + " Numeric(10,3), ";
            }
            else if (dc.DataType.ToString() == "System.Int32")
            {
                strSQL += dc.ColumnName + " int, ";
            }
        }
        strSQL += ")";

         string strStatus = Executesql(strSQL);
            if (strStatus == "Table Created")
            {
                int iCntRecords = 0;
                foreach (DataRow dr in dt.Rows)
                {
                    strSQL = "insert into " + txtTabelName.Text + " values ("; //Inserting value to Table
                    foreach (DataColumn dc2 in dt.Columns)
                    {
                        if (dc2.DataType.ToString() == "System.String")
                        {
                            strSQL += "'" + dr[dc2.Ordinal].ToString().Replace("'", "") + "',";
                        }
                        else
                        {
                            strSQL += dr[dc2.Ordinal] + ",";
                        }
                    }
                    strSQL = strSQL.Substring(0, strSQL.Length - 1) + ")";

                    Executesql(strSQL);
                    iCntRecords += 1; //add n counter on each successfull enter
                }
                MessageBox.Show("Completed! " + Environment.NewLine + Environment.NewLine + iCntRecords.ToString() + " records added!", "Done!");
            }
            else
            {
                MessageBox.Show(strStatus);
            }
        }
        catch (Exception ex)
        {

            MessageBox.Show(ex.ToString());

        }
    }
    private string Executesql(string strSQL)
    {
        try
        {
            SqlConnection con = new SqlConnection(Properties.Settings.Default.connectionstring); //Connection to SQL Database
            con.Open();
            SqlCommand cmd = new SqlCommand(strSQL, con);
            cmd.ExecuteNonQuery();
            con.Close();
        }
        catch (Exception ex)
        {
            return ex.Message.ToString();

        }
        return "Table Created";
    }
}
于 2015-06-25T05:11:00.253 回答