Generics question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?QnJhdmVzQ2hhcm0=?=

    Generics question

    I am trying to convert a class I have to generics and I can't seem to find
    any possible why to implement it. I'm beginning to think I'm doing something
    I shouldn't or I hit generics limitation.

    Here is an example of the old way:

    static void Main(string[] args)
    {
    MonitorCenter monitor = new MonitorCenter() ;
    monitor.SetMoni tor(new ExceptionMonito r());
    monitor.SetMoni tor(new StringMonitor() );
    }
    }

    public class MonitorCenter
    {
    public void SetMonitor(IMon itor monitor)
    {
    _monitors.Add(m onitor);
    }

    public Collection<obje ctExecuteAllMon itors()
    {
    Collection<obje ctallMonitorRet urns = new Collection<obje ct>();
    foreach (IMonitor monitor in _monitors)
    {
    Collection<obje ctobjs = monitor.Execute Monitor();
    foreach (object obj in objs)
    allMonitorRetur ns.Add(obj);
    }

    return allMonitorRetur ns;
    }

    Collection<obje ct_monitors = new Collection<obje ct>();
    }


    public interface IMonitor
    {
    Collection<obje ctExecuteMonito r();
    }

    public class ExceptionMonito r : IMonitor
    {
    public Collection<obje ctExecuteMonito r()
    {
    Collection<obje ctexceptions = new Collection<obje ct>();
    exceptions.Add( new Exception("exce ption test"));
    return exceptions;
    }
    }

    public class StringMonitor : IMonitor
    {
    public Collection<obje ctExecuteMonito r()
    {
    Collection<obje ctmsgs = new Collection<obje ct>();
    msgs.Add("strin g test");
    return msgs;
    }
    }


    Here is the new way which for obvious reasons it will never compile:

    public class MonitorCenter
    {
    public void SetMonitor<T>(I Monitor<Tmonito r)
    {
    _monitors.Add(m onitor);
    }

    public Collection<TExe cuteAllMonitors ()
    {
    //once again because I want to have
    //different types in the collection,
    //I can't use this.
    Collection<Tall MonitorReturns = new Collection<T>() ;
    foreach (IMonitor<Tmoni tor in _monitors)
    {
    Collection<Tobj s = monitor.Execute Monitor();
    foreach (T obj in objs)
    allMonitorRetur ns.Add(obj);
    }

    return allMonitorRetur ns;
    }

    //don't know what type to make T???
    Collection<T_mo nitors = new Collection<T>() ;
    }

    public interface IMonitor<T>
    {
    Collection<TExe cuteMonitor();
    }

    public class ExceptionMonito r : IMonitor<Except ion>
    {
    public Collection<Exce ptionExecuteMon itor()
    {
    Collection<Exce ptionexceptions = new Collection<Exce ption>();
    exceptions.Add( new Exception("exce ption test"));
    return exceptions;
    }
    }

    public class StringMonitor : IMonitor<String >
    {
    public Collection<stri ngExecuteMonito r()
    {
    Collection<stri ngmsgs = new Collection<stri ng>();
    msgs.Add("strin g test");
    return msgs;
    }
    }

    Seems like the only why I would be able to make this work is my
    MonitorCenter a generic type which limits me to 1 type per instance, which I
    don't want. Anyway, if anyone know a way around my issue please let me know.
    However, i'm starting to think I will not be able to turn IMonitor into a
    generic type.
  • Jon Skeet [C# MVP]

    #2
    Re: Generics question

    BravesCharm <BravesCharm@di scussions.micro soft.comwrote:
    I am trying to convert a class I have to generics and I can't seem to find
    any possible why to implement it. I'm beginning to think I'm doing something
    I shouldn't or I hit generics limitation.
    <snip>
    Seems like the only why I would be able to make this work is my
    MonitorCenter a generic type which limits me to 1 type per instance, which I
    don't want. Anyway, if anyone know a way around my issue please let me know.
    However, i'm starting to think I will not be able to turn IMonitor into a
    generic type.
    Well, you ought to ask yourself to start with what kind of type safety
    you want to achieve. If you want all the monitors to return the same
    kind of object, then you *do* want to limit one type per MonitorCenter
    instance. If you don't want to enforce that, then I'm not sure I see
    what you can achieve with generics.

    One point, however: the name SetMonitor is very deceptive when it
    *adds* one instead of *setting* one.

    --
    Jon Skeet - <skeet@pobox.co m>
    Web site: http://www.pobox.com/~skeet
    Blog: http://www.msmvps.com/jon.skeet
    C# in Depth: http://csharpindepth.com

    Comment

    • =?Utf-8?B?QnJhdmVzQ2hhcm0=?=

      #3
      Re: Generics question

      I see your point. I guess what I was trying to achieve is making the object
      inheriting off IMonitor<Tstron gly-typed but not when interfacing with it
      with MonitorCenter. I guess I wanted my cake and eat it too.

      As far as SetMonitor, your right it should be AddMonitor!

      Thanks
      Michael

      "Jon Skeet [C# MVP]" wrote:
      BravesCharm <BravesCharm@di scussions.micro soft.comwrote:
      I am trying to convert a class I have to generics and I can't seem to find
      any possible why to implement it. I'm beginning to think I'm doing something
      I shouldn't or I hit generics limitation.
      >
      <snip>
      >
      Seems like the only why I would be able to make this work is my
      MonitorCenter a generic type which limits me to 1 type per instance, which I
      don't want. Anyway, if anyone know a way around my issue please let me know.
      However, i'm starting to think I will not be able to turn IMonitor into a
      generic type.
      >
      Well, you ought to ask yourself to start with what kind of type safety
      you want to achieve. If you want all the monitors to return the same
      kind of object, then you *do* want to limit one type per MonitorCenter
      instance. If you don't want to enforce that, then I'm not sure I see
      what you can achieve with generics.
      >
      One point, however: the name SetMonitor is very deceptive when it
      *adds* one instead of *setting* one.
      >
      --
      Jon Skeet - <skeet@pobox.co m>
      Web site: http://www.pobox.com/~skeet
      Blog: http://www.msmvps.com/jon.skeet
      C# in Depth: http://csharpindepth.com
      >

      Comment

      Working...