namespace use.

M

Mr. X.

Hello.
I have a class : MyClass.
The project is : MyProject.

In the class code :
....
namespace MyProject {
Class MyClass
{
public static void MyFunction()
{
... do something
}
}
}

When using MyClass I do in the calling module :
using MyProject;
....

MyClass.MyFunction() ...

I want to make the code shorted, so I shall write :
MyFunction()

(Without using MyClass.)

How can I do that for static functions ?

Thanks :)
 
J

Jeff Johnson

Mr. X. said:
Hello.
I have a class : MyClass.
The project is : MyProject.

In the class code :
...
namespace MyProject {
Class MyClass {
public static void MyFunction()
{
... do something
}
}
}

When using MyClass I do in the calling module :
using MyProject; ...

MyClass.MyFunction() ...

I want to make the code shorted, so I shall write :
MyFunction()

(Without using MyClass.)

How can I do that for static functions ?

You can't. Static methods are attached to classes. You must state which
class you're calling the method against. I have a feeling you're trying to
duplicate VB's "global" methods that are defined in modules. C# has no such
thing.
 
M

Mr. X.

static is the old shared in VB.NET.
Indeed I want some global methods, I can use.
How can I write some global methods in C# instead of the ones I did ?

Thanks :)
 
J

Jeff Johnson

static is the old shared in VB.NET.
Indeed I want some global methods, I can use.
How can I write some global methods in C# instead of the ones I did ?

I'll say it again: YOU CAN'T. C# does not support this concept. At all.
Every method in C# MUST be in the context of a class.
 
T

Tom Shelton

Mr. X. wrote :
static is the old shared in VB.NET.
Indeed I want some global methods, I can use.
How can I write some global methods in C# instead of the ones I did ?

Thanks :)

As Jeff said, you can't... The closest you can get is to create a
static class:

static class MyUtilities
{
public static void UtilityMethod1()
{
// do cool stuff
}
}


class Program
{
public static void Main()
{
MyUtilities.UtilityMethod1();
}
}
 
M

Mr. X.

O.K
I meant static, as my first question on this thread
(That was how to write : UtilityMethod1(), instead of
MyUtilities.UtilityMethod1().
I guess there is no solution for that).

Thanks, anyway :)
 
T

Tom Shelton

Mr. X. wrote on 7/20/2010 :
O.K
I meant static, as my first question on this thread
(That was how to write : UtilityMethod1(), instead of
MyUtilities.UtilityMethod1().
I guess there is no solution for that).

Thanks, anyway :)

Thank goodness! When I'm forced into using VB, I go out of my way to
make sure you can't do that :) How? I don't use modules...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top