PC Review


Reply
Thread Tools Rate Thread

best method for common functions

 
 
R.A.F.
Guest
Posts: n/a
 
      21st Nov 2007
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
 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?G=F6ran_Andersson?=
Guest
Posts: n/a
 
      21st Nov 2007
R.A.F. wrote:
> 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.

--
Göran Andersson
_____
http://www.guffa.com
 
Reply With Quote
 
Peter Duniho
Guest
Posts: n/a
 
      21st Nov 2007
On 2007-11-21 00:01:47 -0800, Göran Andersson <(E-Mail Removed)> said:

> 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

 
Reply With Quote
 
Kevin.Li
Guest
Posts: n/a
 
      21st Nov 2007
On Nov 21, 4:27 pm, Peter Duniho <NpOeStPe...@NnOwSlPiAnMk.com> wrote:
> On 2007-11-21 00:01:47 -0800, Göran Andersson <gu...@guffa.com> said:
>
> > 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- Hide quoted text -
>
> - Show quoted text -


Thanks, Pete. Your post is really helpful.
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Common Functions and Subs =?Utf-8?B?cmJub3J0aA==?= Microsoft Excel Misc 3 15th Feb 2006 10:07 PM
Common method of using tab control? Brett Romero Microsoft C# .NET 3 18th Jan 2006 09:46 PM
Common Web functions dll (Used to use MTS, what do I use in .NET =?Utf-8?B?SmVmZiBLZXNpbmdlcg==?= Microsoft ASP .NET 4 30th Apr 2005 01:14 AM
Common Functions Pete via DotNetMonster.com Microsoft ASP .NET 3 15th Mar 2005 03:32 PM
common functions:where to put them? matthewgdodds@hotmail.com Microsoft Excel Programming 2 11th Dec 2004 10:38 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:34 AM.