Hello,
I'm trying to use the new C#(2.0) feature : Generic class.
I would like to send to the class a struct variables type, and then after inside the generic class call a struct field that is common to all structure that could be passed.
Exemple that is not working :
public Struct PositionColored
{
public static readonly VertexFormats Format = VertexFormats.Position | VertexFormats.Diffuse;
}
public Struct PositionTextured
{
public static readonly VertexFormats Format = VertexFormats.Position | VertexFormats.Texture1;
}
Public GenericClass<MyStructType>
{
//I would like inside the class to be able to do this type of things :
MyStructType[] StructTab;
// Be able to use this somewhere inside this class (It's that, that's not working)
MyStructType.Format
}
Using interface is not possible as I can't specified field in interface (only method or properties)
Tx you for your help !

Struct and Generic Class
Peter Mackay
The structures are independent types and therefore, even though they have the same name, are not the same fields and can not be treated as the same by the compiler. Your only choice is to use an interface or create specialized versions for the generic class. You're right that you can't use a field but that should be okay. Fields are implementation details anyway.
An alternative, although slow, would be to use reflection inside your generic class to retrieve the values. Unfortunately it is slow and error prone. There is no way you can force a user to pass one of the supported types to your generic class. The constraint system doesn't support constraining value types at this time (beyond whether the constraint must be a reference type or not).
Michael Taylor - 8/2/06
AlexBB
The warning is telling you that you are using the value of a local variable that was never initialized. Therefore your GetFormat() will return whatever the value is from the associated type. You should probably create an instance of the type inside your generic class to ensure it is initialized. This will make the warning go away.
Other than that since your structs now implement the interface the code will work correctly. Note however that you can also define classes that implement the interface and it'll work. In fact you should remove the MyStructType type altogether and simply use the IVertextFormat type instead as this is the only type info you can access.
public class GenericClass<MyStructType> where MyStructType: IVertexFormat, new()
{
// That's the "trick", use a variable inside the code to retrieve the format
private IVertexFormat _DummyStructVariable = new MyStructType();
//I would like inside the class to be able to do this type of things :
MyStructType[] StructTab;
public VertexFormats Format ( )
{
// This is working now !!
return _DummyStructVariable.GetFormat();
}
}
Michael Taylor - 8/2/06
centexbi
Hello again !
It seems that I have found a way to make it works !
Could some C# guru tell me if it's "a good practice" or if I could do it better an other way Tx you !
Here is the code :
public Interface IVertexFormat
{
VertexFormats GetFormat();
}
public Struct PositionColored:IVertexFormat
{
public static readonly VertexFormats Format = VertexFormats.Position | VertexFormats.Diffuse;
public VertexFormats GetFormat()
{
return Format;
}
}
public Struct PositionTextured:IVertexFormat
{
public static readonly VertexFormats Format = VertexFormats.Position | VertexFormats.Texture1;
public VertexFormats GetFormat()
{
return Format;
}
}
Public GenericClass<MyStructType> where MyStructType: IVertexFormat
{
// That's the "trick", use a variable inside the code to retrieve the format
private MyStructType _DummyStructVariable
//I would like inside the class to be able to do this type of things :
MyStructType[] StructTab;
// This is working now !!
_DummyStructVariable.GetFormat();
}
Good practice or not
No matter what, it saves me to have to implement a different GenericClass class for every structure type !
The only "problem" left, is that i keep on receiving a Warning message that say that my _DummyStructVariable is not initialized A way to make this warning go
Tx you for your help !
Samer Selo
Tx you for your quick answer !
This could resolved my "porblems"
I have been told, to never "write" values to a structure through his interface, mainly because the structure is Value type and Interface Reference type.
It could cause some problems. But I must say I'm quite "lost" in it :)
More info about it there : http://blogs.msdn.com/abhinaba/archive/2005/10/05/477238.aspx
That's why I was not using the interface but the object itself.