473,396 Members | 2,090 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,396 software developers and data experts.

Why type parameter cannot used to call static method

I tried to use type parameter to call static method but got compile error
'T' is a 'type parameter', which is not valid in the given context.

Why this is not allowed ?
Should I really use a lot longer ModelGenericBase<T>.test1() ?

Andrus.

class VirtualGrid<T>
where T : ModelGenericBase<T{

void test() {
//'T' is a 'type parameter', which is not valid in the given context
T.test1();
}
}

class ModelGenericBase<T{
public static void test1() {
}
}
Oct 7 '07 #1
4 7618
Andrus wrote:
I tried to use type parameter to call static method but got compile error
'T' is a 'type parameter', which is not valid in the given context.

Why this is not allowed ?
Should I really use a lot longer ModelGenericBase<T>.test1() ?

Andrus.

class VirtualGrid<T>
where T : ModelGenericBase<T{

void test() {
//'T' is a 'type parameter', which is not valid in the given context
T.test1();
}
}

class ModelGenericBase<T{
public static void test1() {
}
}
You can't use the type parameter to call a static method, as there is no
way of specifying in the class header that the type has to have that method.

--
Göran Andersson
_____
http://www.guffa.com
Oct 7 '07 #2
Hi Andrus,

"Andrus" <ko********@hot.eeschrieb im Newsbeitrag
news:ur*************@TK2MSFTNGP02.phx.gbl...
>I tried to use type parameter to call static method but got compile error
'T' is a 'type parameter', which is not valid in the given context.

Why this is not allowed ?
Should I really use a lot longer ModelGenericBase<T>.test1() ?
Yes, indeed. Why don't you want to do that?
Only readability?
C# doesn't support polymorphism of static members, so there would not be a
semantical difference.

For readability you could use a using alias directive.

Christof

<snip>
>
class VirtualGrid<T>
where T : ModelGenericBase<T{

void test() {
//'T' is a 'type parameter', which is not valid in the given context
T.test1();
}
}

class ModelGenericBase<T{
public static void test1() {
}
}
Oct 8 '07 #3

"Göran Andersson" <gu***@guffa.comkirjutas sõnumis
news:%2****************@TK2MSFTNGP05.phx.gbl...
Andrus wrote:
>I tried to use type parameter to call static method but got compile error
'T' is a 'type parameter', which is not valid in the given context.

Why this is not allowed ?
Should I really use a lot longer ModelGenericBase<T>.test1() ?

Andrus.

class VirtualGrid<T>
where T : ModelGenericBase<T{

void test() {
//'T' is a 'type parameter', which is not valid in the given context
T.test1();
}
}

class ModelGenericBase<T{
public static void test1() {
}
}

You can't use the type parameter to call a static method, as there is no
way of specifying in the class header that the type has to have that
method.
I specified in class header that this type has this method:

where T : ModelGenericBase<T>

Is this bug ?

Andrus.
Oct 8 '07 #4
Andrus wrote:
>You can't use the type parameter to call a static method, as there is no
way of specifying in the class header that the type has to have that
method.

I specified in class header that this type has this method:

where T : ModelGenericBase<T>

Is this bug ?

Andrus.
You specified that the type has to be of the type ModelGenericBase<Tor
inherit the type ModelGenericBase<T>. As static methods are specific to
the type and are not inherited, the type doesn't have to have the static
method if it only inherits ModelGenericBase<T>.

--
Göran Andersson
_____
http://www.guffa.com
Oct 8 '07 #5

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

Similar topics

59
by: Michael C | last post by:
eg void DoIt() { int i = FromString("1"); double d = FromString("1.1"); } int FromString(string SomeValue) {
10
by: steve bull | last post by:
I have a class SwatchPanel which takes Swatch as a parameter type. How can I call a static function within the Swatch class? For example the code below fails on TSwatch.Exists. How can I get the...
1
by: tbandrow | last post by:
I would like to get this to compile: public class GenericCtor<T> where T: class, new() { public static T MakeIt() { return new T(1, 2); } }
669
by: Xah Lee | last post by:
in March, i posted a essay “What is Expressiveness in a Computer Languageâ€, archived at: http://xahlee.org/perl-python/what_is_expresiveness.html I was informed then that there is a academic...
4
by: Charles Churchill | last post by:
I apologize if this question has been asked before, but after about half an hour of searching I haven't been able to find an answer online. My code is beloiw, with comments pertaining to my...
9
by: weirdwoolly | last post by:
Hopefully someone will be able to help. I have written a stored procedure in C++ called from a Java test harness to validate the graphic data types in C++ and their use. I have declared the...
9
by: Steve Richter | last post by:
in a generic class, can I code the class so that I can call a static method of the generic class T? In the ConvertFrom method of the generic TypeConvert class I want to write, I have a call to...
4
by: sheperson | last post by:
Hi, I have a stored procedure in my database and it has an argument of type output (int). When I create a dataset using VS2005, it creates a table adapter for me and the Fill method has and...
10
by: Dom Jackson | last post by:
I have a program which crashes when: 1 - I use static_cast to turn a base type pointer into a pointer to a derived type 2 - I use this new pointer to call a function in an object of the...
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: 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...
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...

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.