how to declare global function or method using c#?

后端 未结 4 441
逝去的感伤
逝去的感伤 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:33

    You could create a static class.

    namespace MyNamespace
    {
        public static class MyGlobalClass
        {
            public static void MyMethod() { ... }
        }
    }
    

    You would then add the namespace in the using section of your calling class to access it. Like this:

    using MyNamespace;
    
    public class CallingClass
    {
        public  void CallingMethod()
        {
            MyGlobalClass.MyMethod();
        }
    }
    
    0 讨论(0)
  • 2021-01-04 12:34

    You can create a static class (even enclose it in it's own namespace so as not to pollute the main project namespace), then call it from anywhere:

    namespace SomeNamespace
    {
        public static class SomeClass
        {
            public static string SomeMethod() 
            {
                ...
            }
        }
    }
    

    Then, in your code, you can call it using:

    string x = SomeNamespace.SomeClass.SomeMethod();
    

    Or, you can set up a using at the top of the code and just reference it without the namespace:

    using SomeNamespace;
    ...
    string x = SomeClass.SomeMethod();
    
    0 讨论(0)
  • 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<Person> GetPersons(string query, string connectionString)
            {
                List<Person> dbItems = new List<Person>();
    
                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<Person> ItStaff = moduleTran.GetPersons(sqlQuery, conString);
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-04 12:50

    If you're using C# 6.0 or later, you could use using static.

    For example,

    using static ConsoleApplication.Developer;
    
    namespace ConsoleApplication
    {
        class Program
        {
            static void Main(string[] args)
            {
                // Global static function, static shorthand really 
                DeveloperIsBorn(firstName: "Foo", lastname: "Bar")
                    .MakesAwesomeApp()
                    .Retires();
            }
        }
    }
    
    namespace ConsoleApplication
    {
        class Developer
        {
            public static Developer DeveloperIsBorn(string firstName, string lastname)
            {
                return new Developer();
            }
    
            public Developer MakesAwesomeApp()
            {
                return this;
            }
    
            public Developer InsertsRecordsIntoDatabaseForLiving()
            {
                return this;
            }
    
            public void Retires()
            {
               // Not really
            }        
        }
    }
    

    One more example:

    using static System.Console;
    
    namespace ConsoleApplication7
    {
        class Program
        {
            static void Main(string[] args)
            {
                WriteLine("test");
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题