1

I am trying to figure out the easiest method to create/convert multiple classes and have them created under the proper sub-classes.

Below is the code for my Event Class

public class Event
{
    public Dictionary<string, string> dictionary = new Dictionary<string, string>();

    public Event(string[] headers, string[] values)
    {
        if (headers.Length != values.Length)
            throw new Exception("Length of headers does not match length of values");

        for (int i = 0; i < values.Length; i++)
            dictionary.Add(headers[i], values[i]);
    }

    public override string ToString()
    {
        return dictionary.ToString();
    }

    public string getTimeStamp()
    {
        return DateTime.Parse(dictionary["event_ts"]).ToLocalTime().ToString();
    }

    public string getKey()
    {
        return dictionary["hbase_key"];
    }

    public string getEventType()
    {
        return dictionary["event_type"];
    }

}

Beyond that, I am going to have multiple classes that are extensions of the Event Class that define more methods for different types of Events. However, I need an easy way based off the EventType to create the proper class. For example if event_type = testEvent I will need to use my TestEvent class. Is there some sort of method I can put in Event that will parse the event_type and figure out which class it should create.

All the information I am getting is from parsing a CSV File with the headers as the first line and the values is the specific row's values.

7H3LaughingMan
  • 372
  • 1
  • 15
  • 4
    Using the Factory pattern might be suitable for your needs - see this related SO article http://stackoverflow.com/questions/10513086/why-use-abstract-factory-pattern-in-c-sharp – Kevin Brady Feb 02 '15 at 01:26
  • 4
    Please. Also for the love of god, read this [Naming Conventions](https://msdn.microsoft.com/en-us/library/ms229045%28v=vs.110%29.aspx). Also, PLEASE use properties instead of `get`/`set` methods, this isn't some barbaric language like Java, without first class support for properties. – Aron Feb 02 '15 at 01:35
  • Can you explain why you need sub classes. It sounds like your Event class is acting as a datarow. So you pull data from a csv and now have a class with an array of headers and an array of values. What behavior do you need to encapsulate in a specialized class? Could you build something like a strategy pattern or state pattern that works on the data you have retrieved? – Noel Feb 02 '15 at 05:46

2 Answers2

0

You can use reflection to match the class type with the event type and then instantiate it using:

Activator.CreateInstance(eventType) as Event
DoronG
  • 2,576
  • 16
  • 22
  • Thank you very much for this, took me a little bit on how to figure out how to use this method in my case. However, it is the best method of having to deal with ~400 Events and doing it another way would result in the same amount of if statements being needed. – 7H3LaughingMan Feb 02 '15 at 20:44
0

You can use 'Factory Method'.

public Event CreateEvent(string sEventType)
{
    if (sEventType.Equals("Event1"))
        return new Event1();
    if (sEventType.Equals("Event2"))
        return new Event2();
    if (sEventType.Equals("Event3"))
        return new Event3();
    //and so on...
}

Event1, Event2 and Event3 are your subclasses. You would need to parse and call this kind of a method.

skmic
  • 79
  • 1
  • 9