Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Passing generics to non generic classes

Passing generics to non generic classes

Scheduled Pinned Locked Moved C#
questioncsshelp
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    David Wengier
    wrote on last edited by
    #1

    I have a class that uses generics to have a typesafe DataSource property. I want to pass an instance of this class to a form and have it display the datasource. The code looks something like this:

    public class DataSource<ItemType> where ItemType : StatefulObjectBase
    {
    public ItemType DataSource
    {
    ....
    }

    public void Show()
    {
        DisplayForm f = new DisplayForm(this);
    }
    

    }

    The question is, how do I code the parameter on the forms constructor. Using DataSource<StatefulObjectBase> doesnt work, even though the compiler should know that whatever is in the object is convertible to that type. Even using object didnt work. All I want to do from the form is access some methods from my generic object. I dont care what type they are. Any help would be much appreciated. EDIT: oops, fixing generics.. damn less than and great thans -- Dave

    W 1 Reply Last reply
    0
    • D David Wengier

      I have a class that uses generics to have a typesafe DataSource property. I want to pass an instance of this class to a form and have it display the datasource. The code looks something like this:

      public class DataSource<ItemType> where ItemType : StatefulObjectBase
      {
      public ItemType DataSource
      {
      ....
      }

      public void Show()
      {
          DisplayForm f = new DisplayForm(this);
      }
      

      }

      The question is, how do I code the parameter on the forms constructor. Using DataSource<StatefulObjectBase> doesnt work, even though the compiler should know that whatever is in the object is convertible to that type. Even using object didnt work. All I want to do from the form is access some methods from my generic object. I dont care what type they are. Any help would be much appreciated. EDIT: oops, fixing generics.. damn less than and great thans -- Dave

      W Offline
      W Offline
      Werdna
      wrote on last edited by
      #2

      There are couple problems with this. 1. You cannot have public ItemType DataSource property as DataSource is name of the class 2. C# does supports generic methods, but not generic constructors. You can write your code like: public interface StatefulObjectBase { } public class DataSource where ItemType : StatefulObjectBase { public ItemType TheDataSource { get { return default(ItemType); } } public void Show() { DisplayForm f = new DisplayForm(); f.Show(this); } } public class DisplayForm { public DisplayForm() { } public void Show(DataSource source) where ItemType : StatefulObjectBase { } }

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups