×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

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

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

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

(OP)
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 --> C#

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

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

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

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

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.

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

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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close