C#中的“公共无效”函数
Posted
技术标签:
【中文标题】C#中的“公共无效”函数【英文标题】:"public void" function in c# 【发布时间】:2012-08-28 21:04:28 【问题描述】:谁能告诉我为什么当我尝试声明和使用“public void”函数时,它给了我错误:
Expected class, delegate, enum, interface, or struct
我在一开始就声明了它并正确设置了它,它不会调用我的主体。我已经研究过了,这似乎是可行的。
编辑:
public void receipt();
namespace ConsoleApp
class Progam
static ... Main()
...
public void receipt()
...
所以它需要在“类程序”大括号中?
【问题讨论】:
你应该发布你班级的代码。方法之前的代码行有问题。可能缺少花括号、括号或类似的东西 【参考方案1】:您必须声明包含在类或结构中的方法,因为方法不是根成员。
【讨论】:
对不起,这个网站的新手,我正在从 C++ 过渡到 C#【参考方案2】:我假设您正在尝试声明一个不包含类(或结构)主体的函数。请注意,在 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()
...
【讨论】:
我认为这是问题所在,我刚刚开始从 C++ 过渡到 C#【参考方案3】:从错误看来,您缺少类声明。
你确定你有这样的东西吗:
public class Foo
public void Bar()
...
【讨论】:
【参考方案4】:将public void receipt()
放入一个类(在Program 或新类中)并删除public void receipt();
。
【讨论】:
【参考方案5】: 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";
【讨论】:
以上是关于C#中的“公共无效”函数的主要内容,如果未能解决你的问题,请参考以下文章