473,398 Members | 2,403 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

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

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.
Sep 6 '06 #1
7 1222

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-******@nospam-microsoft.comwrote in message
news:44***********************@news.orange.fr...
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.


Sep 6 '06 #2
Hello Guillaume,

GBI am looking for a way to create a method that will be available to
GBother
GBclasses in my project, but without needing to instanciate the class.
GBI don't want this method to be static (il would be too easy ^^) !
GBFinally, i'd like to call my method by only doing
GB"methodClass.myClass()" without having to instanciate "methodClass"
GBbefore (like form1.ActiveForm or Form1.DefaultFont that do not need
GBto 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
Sep 6 '06 #3
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" <sl***@ipass.neta écrit dans le message de news:
OB**************@TK2MSFTNGP06.phx.gbl...
>
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-******@nospam-microsoft.comwrote in message
news:44***********************@news.orange.fr...
>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.



Sep 6 '06 #4
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]
- mv*@spam.guard.caspershouse.com

"Guillaume BRAUX" <v-******@nospam-microsoft.comwrote in message
news:44***********************@news.orange.fr...
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.

Sep 6 '06 #5
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-******@nospam-microsoft.coma écrit dans le message de
news: 44***********************@news.orange.fr...
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.

Sep 6 '06 #6
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-******@nospam-microsoft.comwrote in message
news:44***********************@news.orange.fr...
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" <sl***@ipass.neta écrit dans le message de news:
OB**************@TK2MSFTNGP06.phx.gbl...
>>
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-******@nospam-microsoft.comwrote in message
news:44***********************@news.orange.fr.. .
>>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.




Sep 6 '06 #7
Guillaume BRAUX <v-******@nospam-microsoft.comwrote:
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 - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 6 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

31
by: Chris S. | last post by:
Is there a purpose for using trailing and leading double underscores for built-in method names? My impression was that underscores are supposed to imply some sort of pseudo-privatization, but would...
4
by: Edvard Majakari | last post by:
Greetings, fellow Pythonistas! I'm about to create three modules. As an avid TDD fan I'd like to create typical 'use-cases' for each of these modules. One of them is rather large, and I wondered...
11
by: Dave Rahardja | last post by:
OK, so I've gotten into a philosophical disagreement with my colleague at work. He is a proponent of the Template Method pattern, i.e.: class foo { public: void bar() { do_bar(); } protected:...
4
by: venk | last post by:
Hi, given below is my interaction with the interpreter.... In one case, i have created the class method using the "famous idiom"... and in the other, i have tried to create it outside the class...
15
by: Carlos Lozano | last post by:
Hi, What is the right way to create an OCX COM component. The component is already registerred, but can't create an instance. I am using the reference to the interop module created. If I use...
6
by: Don Wash | last post by:
Hi All! I'm developing ASP.NET pages using VB.NET language. My background is VB6 and ASP3. Right now, I'm evaluating strategies on creating reusable and common functions and methods for ASP.NET....
18
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
26
by: nyathancha | last post by:
Hi, How Do I create an instance of a derived class from an instance of a base class, essentially wrapping up an existing base class with some additional functionality. The reason I need this is...
2
by: FFrozTT | last post by:
I am having a problem creating a DLL with an entry point. I've been trying sub Main, DllMain, and I get nothing. When I run dumpbin - exports mydll.dll I see no entry points, also the dll when...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.