non-class methods

  • Thread starter Thread starter Keith Henderson
  • Start date Start date
K

Keith Henderson

I know I sound like a dolt for asking, but if I have a few functions that
any objects in my code can call, mostly these are functions, how do I make
them public to the application? I'm confused about whether or not I put
them in a class or not a class. I know in VB there is an object called a
module which just holds "unstructured" or as I see it free-for-all code.

thanks
 
Keith,

You want to make them public static. This will allow any code that can
see the type (which is any code in an assembly that references the assembly
where the type that has this method is contained) to execute it without an
instance (the static part).

A module in VB really compiles to a class with static methods.

Hope this helps.
 
Hi, Keith.
You don't have much of a choice in C#. :) In C#, functions are either
defined in a class, or a struct (a value type). If you want to call the
functions from any where and the functions don't require any instance data,
you can put them into a public class as static member functions. VB.NET
modules are also eventually mapped to .NET CLR classes.

Ming Chen
 
hi Keith

the following code is doing what you want.
make an abstract class so nobody can make instances from it!
make every method in this class static so you dont have to instance an
object of this class for using its methods.

--- CODE ---
public abstract class MyFunctions
{
public static void func1()
{
}
public static int func2(string something)
{
MyFunctions.func1();
}
}

cheers, jazper
 
If you want to stop people instantiating the class this is only going to sort of work

public class Foo : MyFunctions

{

}

MyFunctions f = new Foo();

this will construct your class when the derived one constructs.

To stop anyone constructing just create a provate default constructor

public class MyFunctions

{

private MyFunctions(){}

...

}

In Whidbey we get the concept of a static class from which teh compiler emits a sealed abstract class.

Regards

Richard Blewett - DevelopMentor

http://staff.develop.com/richardb/weblog

nntp://news.microsoft.com/microsoft.public.dotnet.languages.csharp/<#[email protected]>

hi Keith

the following code is doing what you want.
make an abstract class so nobody can make instances from it!
make every method in this class static so you dont have to instance an
object of this class for using its methods.

--- CODE ---
public abstract class MyFunctions
{
public static void func1()
{
}
public static int func2(string something)
{
MyFunctions.func1();
}
}

cheers, jazper



---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.766 / Virus Database: 513 - Release Date: 17/09/2004



[microsoft.public.dotnet.languages.csharp]
 
Thanks group.

I also looked in my data access application block and saw that it started
with

public sealed class _classname_
{
public static _datatype_ _methodname_
{
...
}
}

so sealed can also stop the class from being instantiated. is this right?
 
Keith Henderson said:
Thanks group.

I also looked in my data access application block and saw that it started
with

public sealed class _classname_
{
public static _datatype_ _methodname_
{
...
}
}

so sealed can also stop the class from being instantiated. is this right?

No. Sealed just means it can't be derived from. String is a sealed
class, for instance - but you can still instantiate it.
 
Add a module in the project and create your methods within the module.
This will make your methods globally acessible throughout the project.

with regards,


J.V.Ravichandran
- http://www.geocities.com/
jvravichandran
- http://www.411asp.net/func/search?
qry=Ravichandran+J.V.&cob=aspnetpro
- http://www.southasianoutlook.com
- http://www.MSDNAA.Net
- http://www.csharphelp.com
- http://www.poetry.com/Publications/
display.asp?ID=P3966388&BN=999&PN=2
- Or, just search on "J.V.Ravichandran"
at http://www.Google.com
 
Back
Top