2

I'm new to c# and I was wondering if someone could tell me how to create a global module for changing a form like you would do in vb, and then how to call that module.

Thanks

Update:

Ok so i have multiple forms and instead of writing the same 2 lines over and over, which are..

Form x = new Form1();
x.Show();

Is it possible to add this to a class to make it easier. How do i do it? never made a class before. got the class up but unsure how to write the code.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
jackie
  • 57
  • 1
  • 2
  • 8
  • 1
    C# doesn't have modules. Use classes. – Andrey Jan 20 '11 at 18:34
  • classes with static members are pretty darn close to global modules. namespace.classname.staticmethod( args ); – kenny Jan 20 '11 at 18:38
  • ok thanks for the class advice. how would i write "form.show" that would apply to all forms in the class... if u understand – jackie Jan 20 '11 at 18:45
  • ""form.show" that would apply to all forms in the class"...Ummm please explain what u mean by this.. do you want to open all forms at once... Please clarify.. – Shekhar_Pro Jan 20 '11 at 19:01
  • no just when i click a button the class gets called and opens the form that corresponds with that button – jackie Jan 20 '11 at 19:09

3 Answers3

6

There is nothing as Global Module in C# instead you can create a Public class with Static Members like:

Example:

//In a class file 

namespace Global.Functions //Or whatever you call it
{
  public class Numbers
  {
    private Numbers() {} // Private ctor for class with all static methods.

    public static int MyFunction()
    {
      return 22;
    }
  }
}

//Use it in Other class 
using Global.Functions
int Age = Numbers.MyFunction();
Shekhar_Pro
  • 18,056
  • 9
  • 55
  • 79
2

No, this requires help from a compiler. A VB.NET module gets translated to a class under the hood. Which is very similar to a static class in C#, another construct that doesn't have a real representation in the CLR. Only a compiler could then pretend that the members of such a class belong in the global namespace. This is otherwise a compat feature of the VB.NET compiler, modules were a big deal in the early versions of visual basic.

A static class declared without a namespace is the closest you'll get in C#. Util.Foo(), something like that.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
0

The answer with public class using static MethodName is close. You could take it a step further and make the class static too.

Here is another Stack Overflow answer that talks about it. Classes vs. Modules in VB.Net

Community
  • 1
  • 1
Greg Levenhagen
  • 924
  • 7
  • 14