The class is as follows.....
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
namespace Breakfast3D.Graphics.ContentManager
{
public class ContentObject
{
private string _file;
private string _label;
private Device _device;
private ContentType _content;
public string FilePath { get { return _file; } }
public string Label { get { return _label; } }
public ContentType Content { get { return _content; } }
public ContentObject(Device device, string label, string file)
{
_device = device;
_file = file;
_label = label;
}
public void LoadResource()
{
if (_content is Texture)
_content = TextureLoader.FromFile(_device, _file);
}
public void UnloadResource()
{
}
}
}
The error im recieving is....
Cannot implicitly convert type "Direct3D.Texture" to "ContentType"
( _content = TextureLoader.FromFile(_device, _file); )
The class is instantised with this code...
_contentManager.AddResource
public static void AddResource
{
ContentObject
_content.LoadResource();
_register.Add(label, _content);
}
Any thoughts, I've looked around different sites at tutorials on generics, but most of them used the same tutorial, with the same examples.
Any help greatly appreciated.
AlanPosted Oct 3, 2007, 9:47 AM
Actually, Michael, when I saw you using the 'object' type in your last post, it suddenly dawned on me how the code in your original post could be made to compile, namely to cast the Texture to an object before casting it to ContentType:
if (_content is Texture)
_content = (ContentType)(object)TextureLoader.FromFile(_device, _file);
The compiler should allow this because you can cast any type to an object and an object reference can refer to any type.
MichaelPosted Oct 3, 2007, 7:45 AM
After looking at your last suggestion....
Changed "_content" to an "object" type.
And changed...
public void LoadResource()
{
if (typeof(ContentType) == typeof(Texture))
_content = TextureLoader.FromFile(_device, _file);
}
Which compiled, unfortunately I dont have anything setup to test it yet, will look at that tommorow, getting late now.
Final Code....
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
namespace Breakfast3D.Graphics.ContentManager
{
public class ContentObject
{
private string _file;
private string _label;
private Device _device;
private object _content;
public string FilePath { get { return _file; } }
public string Label { get { return _label; } }
public object Content { get { return _content; } }
public ContentObject(Device device, string label, string file)
{
_device = device;
_file = file;
_label = label;
}
public void LoadResource()
{
if (typeof(ContentType) == typeof(Texture))
_content = TextureLoader.FromFile(_device, _file);
}
public void UnloadResource()
{
}
}
}
MichaelPosted Oct 3, 2007, 7:35 AM
When I first coded this I assumed that since "ContentType" was an undefined data type, that any data type could be assigned to it, without the compiler getting upset. Is the rest of the code correct in the way its setup?
AlanPosted Oct 3, 2007, 7:22 AM
If you change the AddResource() method to the following:
public static void AddResource(string label, string file) _content = new ContentObject(_device, label, file);
{
if (typeof(ContentType) == typeof(Texture))
{
ContentObject
_content.LoadResource();
_register.Add(label, _content);
}
}
place a constraint on the generic class:
public class ContentObject where ContentType : Texture { // code }
and leave the cast as per my first post, then it might compile.
However, even if it does, it's really defeating the point of having a generic class in the first place and it might be better to use a non-generic class which is specialized to deal with Texture objects.
Generics tend to be a bit disappointing in practice :(
MichaelPosted Oct 3, 2007, 5:21 AM
if (_content is Texture)
_content = (ContentType)TextureLoader.FromFile(_device, _file);
also tried....
if (_content is Texture)
(Texture)_content = TextureLoader.FromFile(_device, _file);
no luck, but thank you anyway
AlanPosted Oct 3, 2007, 5:13 AM
Try using a cast:
if (_content is Texture)
_content = (ContentType)TextureLoader.FromFile(_device, _file);