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
        }
    }
8
  • They need to have a common ancestor, like a base class or an interface. Commented Feb 25, 2015 at 13:14
  • Updated my answer ... what you are trying to do is not possible unfortunatly. Commented Feb 25, 2015 at 14:10
  • @Tomo angular js works with json right? but what server technology are you using? Commented Feb 25, 2015 at 14:48
  • @Florian: yes, json. pardon my ignorance, what do you mean with server technology? Commented Feb 25, 2015 at 15:02
  • @Tomo edited again so you see proof that this works... Commented Feb 25, 2015 at 15:03

2 Answers 2

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"}]
Sign up to request clarification or add additional context in comments.

9 Comments

I don't see the connection between your code and string prpClass.
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<Blah> Whatever(string prpClass), if string prpClass = "Doh" then public List<Doh> Whatever(string prpClass) etc. I could duplicate my code a dozen times, but that wouldn't be funny.
So you access a database?
@Tomo where would you get the data from?
Yes, I do. My query depends on that string and I would like to return the appropriate List<>.
|
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"
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.