1

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?

4

2 回答 2

1

我建议Dictionary(Of String, Type)改为 - 将所有信息保存在一个地方,这意味着您可以以编程方式执行映射。当然,您也可以在另一个方向上进行另一个映射 - 并且可以从第一个方向自动生成(例如ToDictionary()在 LINQ 中使用)。

我还强烈建议您去掉cls类名的前缀 - 值得阅读Microsoft 的命名约定,以便您的代码是惯用的。

于 2013-10-08T07:15:45.560 回答
0

TypeOf obj Is YourClass可以确定对象的类别。您将摆脱在类中保留共享变量。

于 2013-10-08T07:25:58.193 回答