Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Where do we put public functions in C# (modules don't exist) ?

Status
Not open for further replies.

WomanPro

Programmer
Nov 1, 2012
180
0
0
GR
In Visual Basic .net we have the modules to write public functions and routines in order to call them from several points of entire application.
What about c# 2019? Modules don't exist in C#. Any suggestions please? Thank you so much in advanced.

Example of a public void function, I have several of them, that I need to call them.
Code:
public void Search(string sql, SqlConnection con)
{
     if (con.State == ConnectionState.Closed)
         con.Open();

     SqlCommand Cmd = new SqlCommand(sql, con);
     Cmd.CommandType = CommandType.Text;
     SqlDataReader Rdr = Cmd.ExecuteReader();

     if (Rdr.HasRows == false)
         MessageBox.Show("H εγγραφή που αναζητάτε δεν υπάρχει! ", "Αναζήτηση ");
    
     while (Rdr.HasRows)
     {
        while (Rdr.Read())
        {
            foreach (Control cntl in this.Controls)
            {
               if (cntl.GetType() == typeof(TextBox))
                 for (int i = 0; i <= Rdr.FieldCount - 1; i++)                            
                    if (cntl.Name.Equals(Rdr.GetName(i)))
                       cntl.Text = Rdr.GetString(i);                                       
             }                 
         }

         Rdr.NextResult();
    }

    saveSqlQ.SQL = sql;    
}
 
You can use a static class and static methods to do this. Ex:

Code:
public static class Anything {

    public static void Search() {
        MessageBox.Show("Did you find it?");
    }
}

Then, anywhere else in your code, you can do this....

Code:
    Anything.Search();

In this example, the "Anything" class will be accessible anywhere from your code, and you don't need to create the class before you use it.



-George
Microsoft SQL Server MVP
My Blogs
SQLCop
twitter
"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Beware of static modules and functions.
They are use shared data space and one call can corrupt another call.
Best to just create a folder in your project and add public classes to it.
each method would also need to be public.
Once you have done that,
1. create an instance of the class
2. call the method..
e.g. public class PublicFunctions{
public string sayHello(string name){
Return $"hello {name}";​
}
}

var pf = new PublicFunctions();
pf.sayHello("no cool");

PS i didnt check to see if it would run, but the concept is good.
In our shop we typically put all our shared/public methods into a separate project, then we setup a project reference where we wish to use those methods.
 
I would suggest couple of solutions:
1. Use Class library, and put there needed functionality
2. From design prospective, make sure to use some kind of dependency injection and mocking mechanism for dealing with TDD.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top