Michael

Michael

  • NA
  • 7
  • 0

C# Generics

Oct 3 2007 4:36 AM
I have just setup my first class using generics, I'm not too sure if I have it set up incorrect, or possibly using it for the wrong purpose.

The class is as follows.....

using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;

namespace Breakfast3D.Graphics.ContentManager
{
    public class ContentObject<ContentType>
    {
        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<Texture>("texture1", "texture1.jpg");

public static void AddResource<ContentType>(string label, string file)
        {
            ContentObject<ContentType> _content = new ContentObject<ContentType>(_device, label, file);
            _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.

Answers (6)