Reputation: 28586
I use a Height
for all the Foo
s members. Like this
public class Foo<T>
{
public static int FoosHeight;
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Foo<???>.FoosHeight = 50; // DO I Set "object" here?
}
}
The same situation is in VB.NET.
Upvotes: 7
Views: 1616
Reputation: 415840
Each specific generic is it's own type, and therefore has it's own static variable. So a Foo<int>
would have a different static height member than a Foo<string>
. If you want that shared among all specific Foo<T>
types, you need to implement that somewhere else.
If you really do just want to set the value for a Foo<object>
type, than it's just that: Foo<object>
.
Upvotes: 2
Reputation: 74909
You need to specify a type, such as
Foo<int>.FoosHeight = 50;
or
Foo<Bar>.FoosHeight = 50;
but each is separate. Foo<int>.FoosHeight
is not related to Foo<Bar>.FoosHeight
. They're effectively two separate classes with two distinct static fields. If you want the value to be the same for all Foo<> then you need a separate place to store it like
FooHelper.FoosHeight = 50;
And FooHelper has no formal relationship with Foo<>.
Upvotes: 7
Reputation: 16505
You'd have to put some generic type parameter in there. That being said, I'll sometimes use some kind of inheritance scheme to get this functionality without having to put in the generic type parameter.
public class Foo
{
public static int FoosHeight;
}
public class Foo<T> : Foo
{
// whatever
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Foo.FoosHeight = 50; // DO I Set "object" here?
}
}
That being said, this will keep the same FoosHeight regardless of the generic type parameter passed into Foo<T>
. If you want a different value for each version of Foo<T>
, you'll have to pick a type to put in that type parameter, and forget the inheritance scheme.
Upvotes: 13