Creating a method that will be availlable to other classes without instanciating the class

  • Thread starter Thread starter Guillaume BRAUX
  • Start date Start date
G

Guillaume BRAUX

Hello,

I am looking for a way to create a method that will be available to other
classes in my project, but without needing to instanciate the class.
I don't want this method to be static (il would be too easy ^^) !

Finally, i'd like to call my method by only doing "methodClass.myClass()"
without having to instanciate "methodClass" before (like form1.ActiveForm or
Form1.DefaultFont that do not need to instanciate form1 to use them ...).

Thanks for your help,

Guillaume,
Paris, FR.
 
its called either a static or shared method (C# then vb.net)

public class EmployeeHelper
{

private EmployeeHelper()
{
//make the constructor private
}

public static int GetEmployeeAgeInYears( Employee e)
{
return ( some date difference function comparing the employee date
of birth, with today's date, in years
}

}


Not a great example, but the syntax is there.

Emphasis on the "public static"
 
Guillaume,

What you want to do can not be done except with a static method. Why
don't you want to use a static method?
 
Hello Guillaume,

GB> I am looking for a way to create a method that will be available to
GB> other
GB> classes in my project, but without needing to instanciate the class.
GB> I don't want this method to be static (il would be too easy ^^) !
GB> Finally, i'd like to call my method by only doing
GB> "methodClass.myClass()" without having to instanciate "methodClass"
GB> before (like form1.ActiveForm or Form1.DefaultFont that do not need
GB> to instanciate form1 to use them ...).

ActiveForm() is just static property, nothing else.


---
WBR,
Michael Nemtsev :: blog: http://spaces.msn.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
 
Thanks for your answer Sloa,

But as I said in my post, I don't whant the method to be "public static".
I use a "this.xxx" statement in this method, and the "this." does not work
in static method.

I know it works greate with "public static", but is there another way to
share a method to all classes, that will accept "this." ?

Thanks,

G
 
I precise that a want to use a "this.xxx" statement in my method, so I
cannot have it static (as "this."do not work in static method ...).

Thanks,

G
 
Using "this" inside a class method implies that the method cannot be static.
If you want your 'static' method to operate on a instance of the class then
use the example below.

class Class1
{
public static int MyStaticMethod(Class1 instance)
{
return instance.xxx();
}
public int xxx()
{ return 12;}
}

main
{
Class1 aCls = new Class1();
int aValue = Class1.MyStaticMethod(aCls);
}
 
Guillaume BRAUX said:
I precise that a want to use a "this.xxx" statement in my method, so I
cannot have it static (as "this."do not work in static method ...).

You've given two contradictory requirements:

1) You want it to be an instance method
2) You want to be able to call it like a static method

Either it's a static method or it's an instance method. It can't be
both.

Now, without any explanation of why you've got these requirements, we
can't give any advice - why do you need "this.xxx" in your method?
Could your static method just call an instance method on a predefined
instance (eg a singleton)?
 

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

Back
Top