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

Generics - Calling Static Method

Bob
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 a Something.

Now I have a collection Things<T>:List<T> where T:Thing. In that
collection's code how can I call Thing's static method?

T.GetThing gives a compile error.

tempThing = new T();
T.GetThing()

won't work 'cos the method is static not instance.

Any ideas?
--
Bob
Nov 17 '05 #1
3 1725
If I understand your problem (doubtful), the method(s) you wish to call are
static. You call them the same way, via "ClassName.methodName", as in
Thing.GetThing(), or SomeThing.GetStaticVariable.

The fact that you have a collection parameterized by T where T:Thing should
not affect the way you call static methods. You are not invoking these
methods against members of the collection, but using the class name (you are
not using a variable of type T or of type Thing as the invoking object).

On the other hand, if you have the problem of two different static methods,
one in class Thing, and one in class Something, and you want to call one or
the other depending on the type of an individual in the collection, there
are a variety of ways to do that. You could have a virtual (i.e. non
static) method in the class, and in the subclass, that calls the correct
static method depending on the class it is implemented in. Then the correct
function would be invoked in code like:

foreach (T t in collection)
t.invokeCorrectStaticMethodViaVirtualFunction()

You could also test explicitly the type of the object, and invoke the static
method (via the class name) depending on the outcome.

I suspect I don't really understand your problem; perhaps you could clarify
it.
"Bob" <bm*****@swiftkenya.com(nospamm)> wrote in message
news:35**********************************@microsof t.com...
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 a Something.

Now I have a collection Things<T>:List<T> where T:Thing. In that
collection's code how can I call Thing's static method?

T.GetThing gives a compile error.

tempThing = new T();
T.GetThing()

won't work 'cos the method is static not instance.

Any ideas?
--
Bob

Nov 17 '05 #2

"Bob" <bm*****@swiftkenya.com(nospamm)> wrote in message
news:35**********************************@microsof t.com...
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 a Something.

Now I have a collection Things<T>:List<T> where T:Thing. In that
collection's code how can I call Thing's static method?

T.GetThing gives a compile error.

tempThing = new T();
T.GetThing()

won't work 'cos the method is static not instance.

Any ideas?


I think you're confusing yourself.

Just call

Thing.GetThing()

David
Nov 17 '05 #3
Hehe. I pondered on this for minutes before I read your reply.
So simple, but it gotme. =)

Happy Coding
- Michael S

"David Browne" <davidbaxterbrowne no potted me**@hotmail.com> wrote in
message news:ui**************@TK2MSFTNGP09.phx.gbl...

"Bob" <bm*****@swiftkenya.com(nospamm)> wrote in message
news:35**********************************@microsof t.com...
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 a Something.

Now I have a collection Things<T>:List<T> where T:Thing. In that
collection's code how can I call Thing's static method?

T.GetThing gives a compile error.

tempThing = new T();
T.GetThing()

won't work 'cos the method is static not instance.

Any ideas?


I think you're confusing yourself.

Just call

Thing.GetThing()

David

Nov 17 '05 #4

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...
16
by: bigtexan | last post by:
I would like to do the following and cannot figure it out. public class A<T> { public delegate T GetValueDelegate(A<T> var); public GetValueDelegate GetValue = new GetValueDelegate(B.GetValue);...
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 {
4
by: taub.adam | last post by:
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() {...
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...
10
by: Marc Gravell | last post by:
Given a generic method "of T", is there a good way of ensuring that any static ctor on T has executed? Following code demonstrates (TestClass1) that via generics you can use the Type instance long...
7
by: SpotNet | last post by:
Hello NewsGroup, Reading up on Generics in the .NET Framework 2.0 using C# 2005 (SP1), I have a question on the application of Generics. Knowingly, Generic classes are contained in the...
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.