how to declare global function or method using c#?

后端 未结 4 440
逝去的感伤
逝去的感伤 2021-01-04 11:56

Can anyone tell me how to declare a global function in c#, similar to what a Module does in VB.net? I need to call a function that can be called in my form1, fo

4条回答
  •  有刺的猬
    2021-01-04 12:38

    @kol is right, there are NO global functions in C#. Take a look at this post MSDN post. I would use layers (I renamed your "Module" class to "TransactionsModule") and It would look like this:

    using System;
    using System.Collections.Generic;
    using System.Data.OleDb;
    
    namespace XYZ
    {
        public class TransactionsModule
        {
            public List GetPersons(string query, string connectionString)
            {
                List dbItems = new List();
    
                OleDbConnection conn = new OleDbConnection(connectionString);
    
                try 
                {
                    conn.Open();
                    var cmd = new OleDbCommand(query, conn);
                    cmd.CommandText = query;
    
                    using (OleDbDataReader reader = cmd.ExecuteReader())
                    {
                        Person objPerson = new Person();
    
                        //These are the columns returned
                        objPerson.Name = Convert.ToString(myReader["Name"]);
                        objPerson.Age = Convert.ToInt32(myReader["Age"]);
    
                        dbItems.Add(objPerson);
                    }
                } 
                catch(OleDbException ex)
                {
                    throw ex;
                }
                finally
                {
                    conn.Close();
                }
                return dbItems;           
            }
    
    
        }
    
        //This class should be in another Layer, but I placed it here since It's a quick Example
        public class Person
        {
            public string Name { get; set; }
            public int Age { get; set; }
        }
    }
    

    All the logic was abstracted to the TransactionsModule class, then you only need to call the Method: GetPersons. Take a look:

    using System;
    using System.Collections.Generic;
    using XYZ.TransactionsModule;
    
    namespace XYZ
    {
        public partial class frmReports : Form
        {      
            public frm1()
            {
                InitializeComponent();
                protected TransactionsModule moduleTran;
            }
    
            private void frm1_Load(object sender, EventArgs e)
            {
                //We initialize the Data Access Layer class
                moduleTran = new TransactionsModule();
    
                //This ConnectionString should be in your app.config
                string conString = "provider= microsoft.jet.oledb.4.0;data source=..\\dbCooperative.mdb";
                string sqlQuery = "SELECT * FROM table";
    
                List ItStaff = moduleTran.GetPersons(sqlQuery, conString);
            }
        }
    }
    

提交回复
热议问题