Macros in .NET

  • Thread starter Thread starter sonali_reddy123
  • Start date Start date
S

sonali_reddy123

Hi all,

My application needs a macro which have scope in whole application
Is it possible to define a macro having global scope.

Thanks in advance.
 
I believe you are refering to static classes/methods, etc. (Static classes in C# is a new 2.0 feature, from what I've seen in BETA
2)

I believe VB.NET already supports static classes, but they are called something different. (Modules, perhaps?)

Here's a 'static' example that works in any framework version (written in C#):

public namespace MyNamespace
{
public sealed class Math // sealed prevents other classes from inheriting "Math"
{
// private constructor prevents classes from creating an instance of "Math"
private Math()
{
}

public static int Add(int v1, int v2) // static keyword means that this method is 'shared' (VB keyword) amoung all
classes that it is visible to
{
return v1 + v2;
}
}
}

// example to use Math (notice, Math already exists in the framework, and is static, but does not have an Add method :)

public class Test
{
static void Main() // entry point to our application (notice, it is "static" which means an instance of "Test" is not
required)
{
// print '3':
Console.WriteLine(MyNamespace.Math.Add(1, 2));

// ^ didn't have to write new Math() to use the "Add" method
}
}
 
My application needs a macro which have scope in whole application
Is it possible to define a macro having global scope.

Macros are a language feature rather than a framework feature - what
language are you using? I expect managed C++ still has macros, but C#
doesn't. Not sure about VB.NET, but I expect it doesn't.

What kind of macro are you thinking of, anyway? Many macros are better
written as normal methods, potentially public static ones.
 
Back
Top