473,397 Members | 1,985 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,397 software developers and data experts.

Generics calling static methods?

Hi,

Can generics be used to call a static method of a class? The below
example gives an error in my generictest class. Anybody know why?

class foo
{
static public void Test() { Console.WriteLine("foo.Test");}
}

class GenericTest<T>
{
public void CallTest() { T.Test();}
}

Feb 27 '06 #1
4 13255
ta*******@gmail.com wrote:
Can generics be used to call a static method of a class?
No - because there's no constraint which can be used to indicate that a
given static method must be available on a type parameter, so the
compiler can't know whether or not the method will be there.

(It's possible that you could have a constraint of T : SomeBaseType
where SomeBaseType defines the method, but in that case you should just
call SomeBaseType.SomeMethod anyway.)
The below
example gives an error in my generictest class. Anybody know why?


What would you expect to happen if someone had called new
GenericTest<string>().CallTest()?

Jon

Feb 27 '06 #2
Hello, ta*******@gmail.com!

ta> Can generics be used to call a static method of a class?

AFAIK no, because generics use instance of specified type( in your case T )

If you want to use specific methods you can define a constraint.
public interface IFoo
{
void Method();
}

public class Foo : IFoo
{
public Foo()
{
}

public void Method()
{
Console.WriteLine("test");
}
}

public class GenericTest<T> where T : IFoo, new()
{
public void CallTest()
{
T t = new T();
t.Method();
}
}

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
Feb 27 '06 #3
Hi,

<ta*******@gmail.com> wrote in message
news:11*********************@u72g2000cwu.googlegro ups.com...
Hi,

Can generics be used to call a static method of a class? The below
example gives an error in my generictest class. Anybody know why?


Remember that you need to use the type to invoke a static method. Generic is
quite the opposite, you do not especify the type until runtime.

Even if the static method is member of the generic constrain you cannot use
it, as you can use a derived type to instantiate and the derived type does
not implement the static method (the base does) .

The only way to do this (in case that you really need to) is using
reflection, you would have to deal with the case that the static method is
not present though.
What are you trying to do?
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

Feb 27 '06 #4
Thanks all for the help!

I have some code that is repeated in a few places and can pretty nicely
be changed to a generic method except for this one call to a different
static method based on the caller.

In my case the simplest solution will be to pass in a delegate as a
parameter (I think :) ). I just thought it may be possible to use the
generic type instead.

Reflection would work too but its a bit heavy for this case.

In answer to what I would expect at runtime for a "bad case". I had
thought the evaluation of the type was done at runtime and therefore
expected a runtime exception if I passed a type that didnt fit my
usage.

-Adam

Feb 27 '06 #5

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

Similar topics

10
by: Ruediger Klaehn | last post by:
Sorry about the harsh language, but I have to vent my anger to somebody who actually understands what I am talking about. Complaining to my girlfriend is not going to produce any meaningful results...
5
by: Andrew | last post by:
Hello All! I'm trying to compile the following code in VS2005 Beta class A { public static void SA() { } } class B<T> where T : A {
3
by: Bob | last post by:
I have an abstract class Thing which has a static method Thing GetThing(). Class Something inherits from Thing. SomeThing supplies static info for GetThing to get stuff from the database to create...
1
by: Vladimir Shiryaev | last post by:
Hello! Exception handling in generics seems to be a bit inconsistent to me. Imagine, I have "MyOwnException" class derived from "ApplicationException". I also have two classes...
4
by: Cedric Rogers | last post by:
I wasn't sure if I could do this. I believe I am stretching the capability of what generics can do for me but here goes. I have a generic delegate defined as public delegate bool...
7
by: Ajeet | last post by:
hi I am having some difficulty in casting using generics. These are the classes. public interface IProvider<PROF> where PROF : IProviderProfile { //Some properties/methods }
3
by: rbjorkquist | last post by:
I am looking for some help figuring out the best way to refactor these two examples. I was thinking of using delegates and/or generics; but have little experience with either. Any sample code...
1
by: JosAH | last post by:
Greetings, Introduction This week I'll write a bit about generics (those funny angular brackets). I need an example and decided to use sets and some of their operations. This weeks' article...
3
by: Filip Zawada | last post by:
Hi, I've encountered a rather interesting language problem. Suppose I've got following types: //class providing usefull static methods public class GoodBase<T> { public static void Met(){};...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
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.