How to declare a global function or method using C #?

Can someone tell me how to declare a global function in C # similar to what it does Moduleon VB.net? I need to call a function that can be called in my form1, form2 and form3.


I have this code:

using System.Data.OleDb;

namespace XYZ
{
    public static class Module
    {
        public static void dbConnection()
        {
            OleDbConnection con = new OleDbConnection();
            con.ConnectionString = "provider= microsoft.jet.oledb.4.0;data source=..\\dbCooperative.mdb";
            con.Open();
        }
    }
}

and form1:

using System.Data.OleDb;
using XYZ;

namespace XYZ
{
    public partial class frmReports : Form
    {
        public frm1()
        {
            InitializeComponent();
        }

        private void frm1_Load(object sender, EventArgs e)
        {
            Module.dbConnection();
            OleDbCommand cm = new OleDbCommand("SELECT * FROM table", con);
        }
    }
}

but I have an error: "The name" con "does not exist in the current context."

+5
source share
4 answers

You can create a static class.

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

Then you must add a namespace in the section of usingyour calling class to access it. Like this:

using MyNamespace;

public class CallingClass
{
    public  void CallingMethod()
    {
        MyGlobalClass.MyMethod();
    }
}
+14
source

If you are using C # 6.0 or later, you can use using static.

For instance,

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");
        }
    }
}
+10

( , ), :

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

, :

string x = SomeNamespace.SomeClass.SomeMethod();

using :

using SomeNamespace;
...
string x = SomeClass.SomeMethod();
+1

@kol , # . MSDN. ( "Module" "TransactionsModule" ), :

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 a quick Example
    public class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }
}

TransactionsModule, : GetPersons. :

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);
        }
    }
}
+1

All Articles