Using a common static value to programatically identify classes

In a reporting application, I have a number of different types of graphs, each of which is output with code in a specific class. Eg. clsPieChart, clsBarChart, etc. Each of those is derived from a base class, clsChartBase.

Now, the report can be put together dynamically, so there is a string ID code in the database for each type of chart, eg. "pie", "bar", etc.

My solution has been to use a common static variable in each class to match it up with the database code, eg:

Class clsPieChart
  Inherits clsChartBase
  Public ReadOnly Shared _ChartID As String = "pie" ' Each class has one of these.

This is how I can identify which class needs to be instantiated to handle that part of the report. Is this a reasonable approach, or is there a better way to do this sort of thing?

Jon Skeet
people
quotationmark

I would suggest having a Dictionary(Of String, Type) instead - that keeps all the information in a single place, and means you can perform the mapping programmatically. You could make another mapping in the other direction as well, of course - and that can be autogenerated from the first (e.g. using ToDictionary() in LINQ).

I'd also strongly advise you to get rid of the cls prefix for your class names - it's worth reading Microsoft's naming conventions so that your code is idiomatic.

people

See more on this question at Stackoverflow