superSport
superSport

Reputation: 33

"public void" function in c#

Can anyone tell me why when I try and declare and use a "public void" function it gives me the error:

 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?

Upvotes: 1

Views: 41943

Answers (5)

Guest1
Guest1

Reputation: 1

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

Upvotes: 0

Maciek Talaska
Maciek Talaska

Reputation: 1638

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.

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#

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

public void receipt() // this needs to be declared inside a class { ... }

Working C# code is:


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

Upvotes: 1

mbm
mbm

Reputation: 955

Put public void receipt() into a class (inside Program or a new class) and remove public void receipt();.

Upvotes: 1

npinti
npinti

Reputation: 52185

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

Upvotes: 0

Freeman
Freeman

Reputation: 5801

You must declare a method contained in a class or struct, because a method is not a root member.

Upvotes: 7

Related Questions