C#中的“public void”函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12259502/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"public void" function in c#
提问by superSport
Can anyone tell me why when I try and declare and use a "public void" function it gives me the error:
谁能告诉我为什么当我尝试声明并使用“public void”函数时,它给了我错误:
Expected class, delegate, enum, interface, or struct
I have it declared at the start and have it set up correctly, and it won't call in my main body. I've researched it and this seems to be the ay to do it.
我在开始时声明并正确设置了它,它不会调用我的主体。我已经研究过了,这似乎是这样做的方法。
Edit:
编辑:
public void receipt();
namespace ConsoleApp
{
class Progam
{
static ... Main()
{
...
}
}
}
public void receipt()
{
...
}
so it needs to be in the "class Program" Braces?
所以它需要在“类程序”大括号中?
回答by Freeman
You must declare a method contained in a class or struct, because a method is not a root member.
您必须声明包含在类或结构中的方法,因为方法不是根成员。
回答by MaciekTalaska
I assume you're trying to declare a function not withing a class (or struct) body. Please mind that in C# every method has to be declared inside a class.
我假设您正在尝试声明一个不包含类(或结构)主体的函数。请注意,在 C# 中,每个方法都必须在类中声明。
Please note, that if you don't want to create an object to be able to call the method, you may declare it as 'static' as follwing:
请注意,如果您不想创建一个能够调用该方法的对象,您可以将其声明为“静态”,如下所示:
public class MyClass
{
public static void MyMethod()
{
Console.WriteLine("Hello World from static method");
}
}
you may use with ease:
您可以轻松使用:
MyClass.MyMethod();
In your case:
在你的情况下:
public void receipt(); // there are no forward declarations in C#
公共无效收据();// C#中没有前向声明
namespace ConsoleApp { class Progam { static ... Main() { ... } } }
命名空间 ConsoleApp { class Progam { static ... Main() { ... } } }
public void receipt() // this needs to be declared inside a class { ... }
public void receive() // 这需要在类中声明 { ... }
Working C# code is:
工作 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()
{ ... }
}
}
回答by npinti
From the error it seems that you are missing the class decleration.
从错误来看,您似乎缺少类声明。
Are you sure you have something like so:
你确定你有这样的事情:
public class Foo
{
public void Bar()
{
...
}
}
回答by mbm
Put public void receipt()into a class (inside Program or a new class) and remove public void receipt();.
放入public void receipt()一个类(在 Program 或新类中)并删除public void receipt();.
回答by Guest1
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";
}
}

