PC Review


Reply
Thread Tools Rate Thread

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

 
 
Guillaume BRAUX
Guest
Posts: n/a
 
      6th Sep 2006
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.


 
Reply With Quote
 
 
 
 
sloan
Guest
Posts: n/a
 
      6th Sep 2006

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 BRAUX" <v-(E-Mail Removed)> wrote in message
news:44ff0328$0$27393$(E-Mail Removed)...
> 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.
>
>



 
Reply With Quote
 
Nicholas Paldino [.NET/C# MVP]
Guest
Posts: n/a
 
      6th Sep 2006
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?


--
- Nicholas Paldino [.NET/C# MVP]
- (E-Mail Removed)

"Guillaume BRAUX" <v-(E-Mail Removed)> wrote in message
news:44ff0328$0$27393$(E-Mail Removed)...
> 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.
>



 
Reply With Quote
 
Michael Nemtsev
Guest
Posts: n/a
 
      6th Sep 2006
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


 
Reply With Quote
 
Guillaume BRAUX
Guest
Posts: n/a
 
      6th Sep 2006
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

"sloan" <(E-Mail Removed)> a écrit dans le message de news:
(E-Mail Removed)...
>
> 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 BRAUX" <v-(E-Mail Removed)> wrote in message
> news:44ff0328$0$27393$(E-Mail Removed)...
>> 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.
>>
>>

>
>



 
Reply With Quote
 
Guillaume BRAUX
Guest
Posts: n/a
 
      6th Sep 2006
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

"Guillaume BRAUX" <v-(E-Mail Removed)> a écrit dans le message de
news: 44ff0328$0$27393$(E-Mail Removed)...
> 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.
>



 
Reply With Quote
 
Noah Sham
Guest
Posts: n/a
 
      6th Sep 2006
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" <v-(E-Mail Removed)> wrote in message
news:44ff060a$0$27399$(E-Mail Removed)...
> 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
>
> "sloan" <(E-Mail Removed)> a écrit dans le message de news:
> (E-Mail Removed)...
>>
>> 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 BRAUX" <v-(E-Mail Removed)> wrote in message
>> news:44ff0328$0$27393$(E-Mail Removed)...
>>> 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.
>>>
>>>

>>
>>

>
>



 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      6th Sep 2006
Guillaume BRAUX <v-(E-Mail Removed)> wrote:
> 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)?

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
 
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
Creating Generic Class from 2 classes tshad Microsoft C# .NET 16 10th Apr 2006 08:51 PM
Creating Generic Class from 2 classes tshad Microsoft ASP .NET 0 4th Apr 2006 12:48 AM
vb.net access public method in nested classes from another class Pj Microsoft VB .NET 2 24th Jan 2005 11:50 AM
Re: Using Class, WithEvents and auto-instanciating Tetsuya Oguma Microsoft Excel Programming 0 5th Sep 2003 03:22 AM
C# classes - how do I refer to a method in a class without instatiating it? kuvpatel Microsoft C# .NET 5 13th Aug 2003 01:00 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:19 AM.