2012-09-04 39 views
1

谁能告诉我,为什么当我尝试声明和使用一个“公共无效”功能,它给我的错误:“公共无效”功能在C#

Expected class, delegate, enum, interface, or struct 

我有它在开始申报,并有它设置正确,它不会在我的主体中调用。我研究过它,这似乎是做到这一点。

编辑:

public void receipt(); 

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

public void receipt() 
{ 
     ... 
} 

所以它需要在 “类节目” 大括号?

+2

请给我们看一些代码。 –

+0

你应该发布你的班级的代码。该方法之前的代码行中存在问题。可能是一个缺少的大括号,括号或这种类型的东西 – Steve

+1

或丢失大括号 –

回答

7

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

+0

对不起,这个网站的新手,我正在从C++过渡到C# – superSport

0

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

请注意,如果你不希望创建一个对象要能够调用该方法,可以声明为“静态”的follwing:


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

您可以轻松使用:

MyClass.MyMethod(); 

你的情况:

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

命名空间ConsoleApp { 类课程校 { 静态...的Main(){ ... } } }

公共无效收据( )//这需要一个类 { ... }内被宣布

工作的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() 
    { ... } 
} 
} 

+1

结构呢? – Oded

+0

我认为这是问题,我刚刚开始从C++过渡到C# – superSport

0

从错误,你似乎错过了类的decleration。

你确定你有一些像这样:

public class Foo 
{ 
    public void Bar() 
    { 
      ... 
    } 
} 
1

public void receipt()成一个类(内部程序或一个新的类),并删除public void receipt();

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"; 
    } 
}