Question

I'd like to pass a property class reference into a method. For example:

Class SQLiteTables
{
    public class tblPersonnel
    {
        public int PsnID { get; set; }
        public string PsnFirstName { get; set; }
        public string PsnMiddleName { get; set; }
        public string PsnLastName { get; set; }
    }
    public class tblSchedules
    {
        public int SchID { get; set; }
        public string SchDescription { get; set; }
        public DateTime SchStartDtm { get; set; }
        public DateTime SchEndDtm { get; set; }

    ...

    public class TableName
    {
        public int Field1 { get; set; }
        public string Field2 { get; set; }
        public string Field3 { get; set; }

        ...

        public string FieldN { get; set; }
    }
}

I would want to create a method something like this:

public void ThisMethod(PropertyClass propertyclassname)
{
        List<propertyclassname> TempList = dbConn.Table<propertyclassname>().ToList<propertyclassname>();
}

And use it like this:

ThisMethod(tblPersonnel);
ThisMethod(tblSchedules);

I'm trying to avoid making multiple methods for each property. I want it to be one reusable method but I can't seem to figure out how. Many thanks in advance!

Was it helpful?

Solution

You should use generics:

public void ThisMethod<T>(T mySet) where T : MySetBaseClass
{
    ...
}

What do you whant to do in the method and who is calling it?

OTHER TIPS

what you are trying to accomplish is not so easy with your actual logic but if you change it to the following may be you solve it

public abstract class PropertySetBase
     {
           public  abstract int Property_int1 { get; set; }
        public abstract string Property1 { get; set; }
        public abstract string Property2 { get; set; }
        public  abstract  string Property3 { get; set; }
     }   

first class

    public class PropertySet1:PropertySetBase
    {

    public override int  Property_int1
{
      get 
    { 

    }
      set 
    { 
    }
}

public override string  Property1
{
      get 
    { 
    }
      set 
    { 

    }
}

public override string  Property2
{
      get 
    { 
    }
      set 
    { 
    }
}

public override string  Property3
{
      get 
    {   
    }
      set 
    { 
    }
}
}

second class

    public class PropertySet2:PropertySetBase
    {

public override int  Property_int1
{
      get 
    { 
    }
      set 
    { 
    }
}

public override string  Property1
{
      get 
    { 
    }
      set 
    { 
    }
}

public override string  Property2
{
      get 
    { 
    }
      set 
    { 
    }
}

public override string  Property3
{
      get 
    { 

    }
      set 
    { 

    }
}
}

and here how your method should written to accept any propertiesclass

public void ThisMethod( PropertySetBase propertyclassname)
{

}

What I think you want to repeatedly use propertyset type and for that you can use IEnumerable

public class PropertySet
{
        public int Property0 { get; set; }
        public string Property1 { get; set; }
        public string Property2 { get; set; }
        public string Property3 { get; set; }
}

use list of this type in your method for as many item you want to use in your method

public void ThisMethod(List<PropertySet> propertyclass)
{

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top