0

I have this:

    public class Blah
    {
        public int id { get; set; }
        public string blahh { get; set; }
    }

    public class Doh
    {
        public int id { get; set; }
        public string dohh { get; set; }
        public string mahh { get; set; }
    }

    public List<???prpClass???> Whatever(string prpClass)

where string prpClass can be "Blah" or "Doh".

I would like the List type to be class Blah or Doh based on what the string prpClass holds.

How can I achieve this?

EDIT:

public List<prpClass??> Whatever(string prpClass)
    {
        using (var ctx = new ApplicationDbContext())
        {
            if (prpClass == "Blah")
            {
                string queryBlah = @"SELECT ... ";

                var result = ctx.Database.SqlQuery<Blah>(queryBlah).ToList();

                return result;
            }
            if (prpClass == "Doh")
            {
                string queryDoh = @"SELECT ... ";

                var result = ctx.Database.SqlQuery<Doh>(queryDoh).ToList();

                return result;
            }

            return null
        }
    }
Tomo
  • 429
  • 1
  • 10
  • 24

2 Answers2

3

you have to have a common supertype:

 public interface IHaveAnId
 {
      int id { get;set; }
 }

public class Blah : IHaveAnId
{
    public int id { get; set; }
    public string blahh { get; set; }
}

public class Doh : IHaveAnId
{
    public int id {get;set;}
    public string dohh { get; set; }
    public string mahh { get; set; }
}

then you can do:

public List<IHaveAnId> TheList = new List<IHaveAnId>();

and in some method:

TheList.Add(new Blah{id=1,blahh = "someValue"});
TheList.Add(new Doh{id =2, dohh = "someValue", mahh = "someotherValue"});

to iterate through the list:

foreach(IHaveAnId item in TheList)
{
    Console.WriteLine("TheList contains an item with id {0}", item.id); 
    //item.id is allowed since you access the property of the class over the interface
}

or to iterate through all Blahs:

foreach(Blah item in TheList.OfType<Blah>())
{
    Console.WriteLine("TheList contains a Blah with id {0} and blahh ='{1}'", item.id, item.blahh);
}

Edit:

the 2 methods and a int field holding the autovalue:

 private int autoValue = 0;     

 public void AddBlah(string blahh)
 {
      TheList.Add(new Blah{id = autovalue++, blahh = blahh});
 }

 public void AddDoh(string dohh, string mahh)
 {
      TheList.Add(new Doh{id = autovalue++, dohh = dohh, mahh = mahh});
 }

Another Edit

 public List<object> Whatever(string prpClass)
 {
    using (var ctx = new ApplicationDbContext())
    {
        if (prpClass == "Blah")
        {
            string queryBlah = @"SELECT ... ";

            var result = ctx.Database.SqlQuery<Blah>(queryBlah).ToList();

            return result.Cast<object>().ToList();
        }
        if (prpClass == "Doh")
        {
            string queryDoh = @"SELECT ... ";

            var result = ctx.Database.SqlQuery<Doh>(queryDoh).ToList();

            return result.Cast<object>.ToList();
        }

        return null;
    }
}

in the view you then have to decide what type it is. In asp.net MVC you can use a display template and use reflection to get a good design. But then i still don't know what technology you are using.

Yet another Edit

TestClass:

public class SomeClass
{
    public string Property { get; set; }
}

Repository:

public static class Repository
{
    public static List<object> Whatever(string prpClass)
    {
        switch (prpClass)
        {
            case "SomeClass":
                return new List<SomeClass>() 
                {
                   new SomeClass{Property = "somestring"},
                   new SomeClass{Property = "someOtherString"}
                }.Cast<object>().ToList();
            default:
                return null;

        }
    }
}

And a controller action in mvc:

 public JsonResult Test(string className)
 {
    return Json(Repository.Whatever("SomeClass"),JsonRequestBehavior.AllowGet);
 }

then i called it with: http://localhost:56619/Home/Test?className=SomeClass

And got the result:

[{"Property":"somestring"},{"Property":"someOtherString"}]
Florian Schmidinger
  • 4,682
  • 2
  • 16
  • 28
  • I don't see the connection between your code and string prpClass. – Tomo Feb 25 '15 at 12:22
  • I have a request from the browser and based on what is in prpClass, I would like to retrieve some data and send it back to the browser. I have a dozen classes like Blah and Doh. What I want to achieve is if string prpClass = "Blah" then public List Whatever(string prpClass), if string prpClass = "Doh" then public List Whatever(string prpClass) etc. I could duplicate my code a dozen times, but that wouldn't be funny. – Tomo Feb 25 '15 at 12:57
  • So you access a database? – Florian Schmidinger Feb 25 '15 at 13:01
  • @Tomo where would you get the data from? – Florian Schmidinger Feb 25 '15 at 13:19
  • Yes, I do. My query depends on that string and I would like to return the appropriate List<>. – Tomo Feb 25 '15 at 13:21
  • @Tomo and you should consider editing your question so it's more specific to the actual problem you have. as you can see 3-4 other users misunderstood the actual problem – Florian Schmidinger Feb 25 '15 at 13:22
  • @Tomo what technology are you using to access the database? – Florian Schmidinger Feb 25 '15 at 13:22
  • and you cant have a strongly typed list based on a string ... you could use reflection to create generic lists but you can only have one return type unless the method is generic but that does not help you.... – Florian Schmidinger Feb 25 '15 at 13:25
  • so the method could return an object which is a supertype by it's self... whats the technology you are using to publish? aspx asp.net mvc etc... – Florian Schmidinger Feb 25 '15 at 13:27
0

Is this what you are trying to do?

public class Blah
{
    public int id { get; set; }
    public string blahh { get; set; }
}

public class Doh
{
    public int id { get; set; }
    public string dohh { get; set; }
    public string mahh { get; set; }
}

class Program
{
    public static List<T> Whatever<T>(int count) where T: new()
    {
        return Enumerable.Range(0, count).Select((i) => new T()).ToList();
    }

    static void Main(string[] args)
    {
        var list=Whatever<Doh>(100);
        // list containts 100 of "Doh"
    }
}
John Alexiou
  • 28,472
  • 11
  • 77
  • 133