locked
How to Define a generic method ? RRS feed

  • Question

  • Dear all,

    I have the following 2 methods :

    1 public void LoadRunningTags(TagnameParameters tags)  
    2         {  
    3             foreach (ItTagname tag in tags)  
    4             {  
    5                 string Tagname = tag.TagName; //"AnalogDevice_004";   
    6                 string Value = tag.Value; //"0";   
    7                 System.Console.WriteLine("Tag name: {0} with Value: {1}", Tagname, Value);  
    8             }  
    9  
    10             
    11         } 

    1 public void LoadNextTags(NextTagnameParameters nxtTags)  
    2         {  
    3                 foreach (ItNextTagname tag in nxtTags)  
    4                 {  
    5                     string Tagname = tag.TagName; //"AnalogDevice_004";   
    6                     string Value = tag.Value; //"0";   
    7  
    8  
    9                     System.Console.WriteLine("Tag name: {0} with Value: {1}", Tagname, Value);  
    10                 }  
    11           
    12         } 

    As you can see only the type pass as parameter is change.

    I am trying to build a generic method out of this 2 function above.
    Is there a way to specify the type as beeing of one or the other ?

    I have try as follow :

    public bool LoadTag<T>(T tagparam) where T:TagnameParameters,NextTagnameParameters

    but it is refusing

    thanks for help
    regards
    serge


    Your experience is build from the one of others
    Tuesday, March 10, 2009 3:31 PM

Answers

All replies

  • You would need to inherit both from a common interface that implements the TagName and the value properties:

    Define your classes like this:

    public interface ITagParameters

    {

        string TagName { get; set; }

        string Value { get; set; }

    }

     

    public class TagnameParameters : ITagParameters

    {

        public string TagName { get; set; }

        public string Value { get; set; }

    }

     

    public class NextTagnameParameters : ITagParameters

    {

        public string TagName { get; set; }

        public string Value { get; set; }

    }

     

    And your method would look like this:

    public void LoadTags<T>(T tag) where T : ITagParameters

    {

        Console.WriteLine("Tag Name: {0} with Value {1}", tag.TagName, tag.Value);

    }


    David Morton - http://blog.davemorton.net/
    Tuesday, March 10, 2009 4:01 PM
    Moderator
  • Thanks for your reply.

    What about then if I need to pass to my generic method either a List of ITagParameters or ITagParameters ?

    wil it become something like :

    public void LoadTags<List<T>>(T tag) where T : ITagParameters

    thnaks


    Your experience is build from the one of others
    Tuesday, March 10, 2009 4:55 PM
  • Close:

    public void LoadTags<T>(List<T> tag) where T : ITagParameters
    David Morton - http://blog.davemorton.net/
    Tuesday, March 10, 2009 5:00 PM
    Moderator
  •  Args, caramba :-)
    thnaks
    Your experience is build from the one of others
    Tuesday, March 10, 2009 5:02 PM