best method for common functions

R

R.A.F.

Hi,

From C++ world, i'm used to have a cpp file, where i placed all common
functions that i could need within different projects.
something like my own "API".

i would like to do something similar under C# but if i create a
namespace, automatically i must after create a class, enum or struct for
example.

so what is the best method, to do something like i did under C++,
without creating class or something similar ?

for example, i had in the past, functions for getting current working
directory, if file exists, functions to resize all controls, and so on...

i do not want to tide them to only 1 *.cs file... i would prefer to
store them in a namespace.

thanks a lot,

RAF
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

R.A.F. said:
Hi,

From C++ world, i'm used to have a cpp file, where i placed all common
functions that i could need within different projects.
something like my own "API".

i would like to do something similar under C# but if i create a
namespace, automatically i must after create a class, enum or struct for
example.

so what is the best method, to do something like i did under C++,
without creating class or something similar ?

for example, i had in the past, functions for getting current working
directory, if file exists, functions to resize all controls, and so on...

i do not want to tide them to only 1 *.cs file... i would prefer to
store them in a namespace.

thanks a lot,

RAF

C# is object oriented, which means that all methods are inside classes.
You can't declare any methods at ground level, as you could in C++.

The using keyword saves you from specifying the namespace and class
everytime you use a method, though. If you have:

namespace MyFancyLibrary {

public static class UsefulUtilities {

public static string GetSomeInfo() { ... }
public static void DoSomethingCool() { ... }

}

}

By putting:

using MyFancyLibrary.UsefulUtilities;

in your file, you can access the GetSomeInfo and DoSomethingCool methods
directly.
 
P

Peter Duniho

C# is object oriented, which means that all methods are inside classes.
You can't declare any methods at ground level, as you could in C++.

The using keyword saves you from specifying the namespace and class
everytime you use a method, though.

Not quite. At least, not as you describe.
If you have:

namespace MyFancyLibrary {

public static class UsefulUtilities {

public static string GetSomeInfo() { ... }
public static void DoSomethingCool() { ... }

}

}

By putting:

using MyFancyLibrary.UsefulUtilities;

in your file, you can access the GetSomeInfo and DoSomethingCool
methods directly.

You can't write the "using" directive as you've described it. Only a
namespace can go into the "using" directive in that way, not a class.

You can, however, create a class name alias with the "using" directive.
It wouldn't be very useful in this scenario, since the best you could
do is replace "UsefulUtilities" with something shorter (and frankly,
with Intellisense who cares how long a single identifier is? :) ). But
it could be done. For example:

using Utils = MyFancyLibrary.UsefulUtilities;

Then you could write:

Utils.GetSomeInfo();

Instead of:

MyFancyLibrary.UsefulUtilities.GetSomeInfo();

or instead of (if you included the namespace with "using"):

using MyFancyLibrary;

UsefulUtilities.GetSomeInfo();

But if you had a bunch of nested namespaces or classes, it could be
very useful since you could alias some long chain of nesting as a
single identifier.

Pete
 
K

Kevin.Li

Not quite. At least, not as you describe.











You can't write the "using" directive as you've described it. Only a
namespace can go into the "using" directive in that way, not a class.

You can, however, create a class name alias with the "using" directive.
It wouldn't be very useful in this scenario, since the best you could
do is replace "UsefulUtilities" with something shorter (and frankly,
with Intellisense who cares how long a single identifier is? :) ). But
it could be done. For example:

using Utils = MyFancyLibrary.UsefulUtilities;

Then you could write:

Utils.GetSomeInfo();

Instead of:

MyFancyLibrary.UsefulUtilities.GetSomeInfo();

or instead of (if you included the namespace with "using"):

using MyFancyLibrary;

UsefulUtilities.GetSomeInfo();

But if you had a bunch of nested namespaces or classes, it could be
very useful since you could alias some long chain of nesting as a
single identifier.

Pete- Hide quoted text -

- Show quoted text -

Thanks, Pete. Your post is really helpful.
 

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