tags:

views:

67

answers:

3

I use a Height for all the Foos 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.

+5  A: 

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.

David Morton
I suppose, the same solution should work in VB.NET too?
serhio
Yes, it should. Same general idea, just rewrite it in VB.NET. Any online code converter should do the trick.
David Morton
A: 

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>.

Joel Coehoorn
I suppose the same situation will be with the Foo<object>, I mean Foo<object> will differ from Foo<string>
serhio
that is correct. It's a different entity in the type system.
Joel Coehoorn
+4  A: 

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<>.

Sam
Very good explanation.
Daniel Straight