1

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!

ELM
  • 529
  • 2
  • 7
  • 19
  • 1
    What do you want to do inside the `ThisMethod` method? – mnieto Feb 14 '14 at 09:52
  • You can create your class through reflection and pass it to the method – mrida Feb 14 '14 at 09:54
  • 4
    Why do you have so many classes which all look the same other than the property names? It's hard to see what bigger problem you're trying to solve. – Jon Skeet Feb 14 '14 at 09:56
  • If I understood it correctly, you need a method which accepts class name as input parameter and set the properties of those class. If it is so, you can use Reflection Concept. – Rangesh Feb 14 '14 at 09:59
  • @JonSkeet Actually the number of properties vary for each class. – ELM Feb 14 '14 at 10:02
  • So you haven't given a representative example, which doesn't help. And you still haven't explained what you're really trying to achieve. It's very hard to help you without more information. Please read http://tinyurl.com/so-hints – Jon Skeet Feb 14 '14 at 10:05

3 Answers3

1

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?

Hugues
  • 26
  • 2
0

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)
{

}
BRAHIM Kamel
  • 13,492
  • 1
  • 36
  • 47
0

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)
{

}
Amit Tiwari
  • 368
  • 1
  • 4