Click here to Skip to main content
15,883,883 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm new to C# programming. I have 3 methods same content but difference types. I want to make one generic method for all of them. But I don't know how to do it. Please help me. Thanks so much...

public class insDatalog
    {
        public ObjectId _id { get; set; }
        public string _date { get; set; }
        public string _lCode { get; set; }
        public string _lName { get; set; }
        public string _stage { get; set; }
        public string _pcName { get; set; }
        public string _serial { get; set; }
        public string _model { get; set; }
        public string _uCode { get; set; }
        public string _uName { get; set; }
        public string _result { get; set; }
    }


public class insUnitLib
    {
        public ObjectId _id { get; set; }
        public string _uCode { get; set; }
        public string _uName { get; set; }
        public string _date { get; set; }
    }


public class insSerialNo
    {
        public ObjectId _id { get; set; }
        public string _serial { get; set; }
    }

public static void DeleteCollectionDataRow<T>(IMongoCollection<T> collection, insUnitLib t) where T: insUnitLib
        {
            collection.DeleteOne(b => b._id == t._id);
        }

public static void DeleteCollectionDataRow<T>(IMongoCollection<T> collection, insDatalog t) where T : insDatalog
        {
            collection.DeleteOne(b => b._id == t._id);
        }

public static void DeleteCollectionDataRow<T>(IMongoCollection<T> collection, insSerialNo t) where T : insSerialNo
        {
            collection.DeleteOne(b => b._id == t._id);
        }


What I have tried:

I have tried to search in the internet but still have no results.
Posted
Updated 25-Nov-17 10:13am

You can try to create an interface and make your three classes implement this interface; then you can place the restriction in your generic method's declaration on the interface itself. This way:
C#
public interface IObjectWithId
{
   ObjectId _id { get; set; }
}

public class insDatalog : IObjectWithId
{
   // ...
}

public class insUnitLib : IObjectWithId
{
   // ...
}
public class insSerialNo : IObjectWithId
{
   // ...
}

public class YourContainerClass
{
   public static void DeleteCollectionDataRow<T>(IMongoCollection<T> collection, T t) where T : IObjectWithId
   {
      collection.DeleteOne(b => b._id.Equals(t._id));
   }
}

It also supposes that you correctly implemented the IEquatable<T> interface in your ObjectId class/struct definition.

You may also rethink your naming scheme as it does not fit very well into the .NET world, but that's more of a "cosmetic" choice remark which you can ignore, eventually.

Hope this helps. Kindly.
 
Share this answer
 
v3
If all of your T classes derived from the same base class or interface, you could use that instead of individual classes in the where T clause (better solution!)

OR, you could just use a single method and chain all the classes together (not really efficient!):
C#
public static void Delete....<T>(IMongoCollection<T> collection, insUnitLib t) where T: insUnitLib, insDatalog, insSerialNo
 
Share this answer
 
v2
Comments
phil.o 25-Nov-17 16:18pm    
I don't think that could work this way, as T type would need to inherit the three classes at the same time, which is for the moment still impossible in C#.
I may be wrong as I did not try that for more than a couple of years now, but the restrictions on a generic type are to be understood with the AND comparator, not the OR one.
:)
Dave Kreskowiak 25-Nov-17 17:47pm    
Thats why I said the 3 classes would have to inherit from a Base class or an interface.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900