Click here to Skip to main content
15,921,697 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello.
I have a 7 classes, Topaz1, Topaz2, Topaz3... Topaz7 that all derive from class "Topaz".
I need to make a function that creates a list of "Topaz" according to a starting number and an ending number. For example, if the starting number is 3 and the ending number is 5 then the list should have elements of Topaz4, Topaz5, and if the starting number is 1 and the ending number is 4 the list will have the elements of the classes Topaz2, Topaz3, Topaz4.
How do I execute such a code? I have literally gone through every idea I possibly had and a long google search.
Posted
Comments
Sergey Alexandrovich Kryukov 20-Jul-14 21:59pm    
What have you tried so far?
Anyway, it would be a good example of really, really bad technique. Why doing it at all, even for an exercise? :-)
—SA
Supraspinatus 20-Jul-14 22:13pm    
tried implementing interfaces. Besides that I couldn't find anything useful that could help me. I don't need a detailed answered I'll be happy if someone just tells me what to type in google so I'll know what direction to look for.
P.S
thanks for the link I'm gonna go through it even though it doesn't solve my problem. And I'm making a crafting calculator for an online game. It was the best way I could think of.
Sergey Alexandrovich Kryukov 20-Jul-14 22:22pm    
You never mentioned the interfaces. Anyway, the idea is bad enough to through it out. If you want to continue this discussion, you need to explain your ultimate goals.
—SA
ZurdoDev 20-Jul-14 22:24pm    
Perhaps use reflection. Or, have each class expose a property. If we understood why we may be able to see a better route.
Sergey Alexandrovich Kryukov 20-Jul-14 22:25pm    
Sure, but it would be a huge abuse. Reflection is designed for doing useful things. And here is the lack of understanding how to do programming... :-)
—SA

1 solution

This strikes me as a very poor way to do things - but it's pretty simple, depending on what exactly you are trying to do.
Assuming you have classes thus:
C#
public abstract class Topaz { }
public class Topaz1 : Topaz { }
public class Topaz2 : Topaz { }
public class Topaz3 : Topaz { }
public class Topaz4 : Topaz { }
public class Topaz5 : Topaz { }
public class Topaz6 : Topaz { }
public class Topaz7 : Topaz { }


Then if you are filtering an existing list of instances to just the types you want:
C#
private List<Topaz> FilterThem(IEnumerable<Topaz> source, int lo, int hi)
    {
    return source.Where(s => InRange(s, lo, hi)).ToList();
    }
private bool InRange(Topaz t, int lo, int hi)
    {
    int n = int.Parse(t.GetType().Name.Substring(5));
    return (lo < n && n <= hi);
    }

If you are trying to create instances, that's a lot more messy:
C#
private List<Topaz> CreateThem(int lo, int hi)
    {
    List<Topaz> list = new List<Topaz>();
    System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
    string name = assembly.FullName.Split(',')[0] + ".Topaz";
    for (int i = lo + 1; i <= hi; i++)
        {
        list.Add((Topaz)assembly.CreateInstance(name + i));
        }
    return list;
    }

But I still think you have made some poor design decisions here!
 
Share this answer
 

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